0

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>
Pixel_95
  • 954
  • 2
  • 9
  • 21
  • Does this answer your question? [WPF UserControl with generic code-behind](https://stackoverflow.com/questions/3811179/wpf-usercontrol-with-generic-code-behind) – Sinatr Apr 23 '21 at 08:40
  • I never tried myself, but I guess [x:TypeArguments](https://learn.microsoft.com/en-us/dotnet/desktop/xaml-services/generics) should do. – Sinatr Apr 23 '21 at 08:42

1 Answers1

1

You need to provide x:TypeArguments directive:

<Window
    x:Class="MyNamespace.Designer"
    x:TypeArguments="src:Region"
    ...
</Window>
Dmitriy Korolev
  • 288
  • 1
  • 10