Pixel_95
Pixel_95

Reputation: 984

Generic Window-Classes

I want to have a generic type of a Window. However, if I implement the <R> into the class definition, it gives me errors, everywhere I reference on the xaml, e.g. at InitializeComponent(); or if I want to access any label or button.

The name 'InitalizeComponent' is not available in the current context

Probably, the reference/linking from the xaml to the code behind does not work properly.
Are there any suggestions, how I can achieve a correct linking to the xaml with generic window classes?

C#

namespace MyNamespace
{
    public partial class Designer<R> : Window, IEventListener
        where R : Region, new()
    {
        ...
    }
}

XAML

<Window
    x:Class="MyNamespace.Designer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:MyNamespace"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="Designer"
    Width="1600"
    Height="1000"
    mc:Ignorable="d">

    ...

</Window>

Upvotes: 0

Views: 49

Answers (1)

Dmitriy Korolev
Dmitriy Korolev

Reputation: 288

You need to provide x:TypeArguments directive:

<Window
    x:Class="MyNamespace.Designer"
    x:TypeArguments="src:Region"
    ...
</Window>

Upvotes: 1

Related Questions