简体   繁体   中英

Using generic base class for WPF window

Why is this:

public abstract class WindowControls<T> : Window

not possible. I can't seem to figure it out.

    public partial class AnglesteelWindow : WindowControls<AngleSteel> {
    private UCListView uc;
    public AnglesteelWindow() {

        InitializeComponent();
        uc = new UCListView();
        uc.SubmitClick += new EventHandler(ButtonPressed);
        this.uc.grid.PreviewMouseLeftButtonUp +=
            new System.Windows.Input.MouseButtonEventHandler(
                 this.MousePressed14<AngleSteel>);
        stkTest.Children.Add(uc);
        uc.amountLabel.Content = "Milimeter";
        uc.grid.ItemsSource = DatabaseLogic.MaterialTable("Anglesteel").DefaultView;
        base.Material(uc, "Anglesteel");
    }
}

I know how generics work, but don't know why it is not possible to make my AnglesteelWindow derive from WindowControls. The error it gives me is the following:

Base class of 'Name of the solution' differs from declared in other parts.

When i look at the so called other part it is the following:

    public partial class AnglesteelWindow : 
          WindowControls<AngleSteel> System.Windows.Markup.IComponentConnector {

This is made in the AnglesteelWindow.gics file. If i remove it from there it makes no difference at all.

Adding on @MichaelMairegger answer, you can reach your goal by creating another non-generic class that inherits from the generic class like this:

public abstract class WindowControlsOfAngleSteel : WindowControls<AngleSteel>
{

}

And make your window class inherit from it like this:

From XAML:

<ns:WindowControlsOfAngleSteel >

</ns:WindowControlsOfAngleSteel >

Where ns is the namespace where WindowControlsOfAngleSteel exists.

In code (optional):

public partial class AnglesteelWindow : WindowControlsOfAngleSteel
{

}

You cannot change the inheritance tree. AnglesteelWindow is partial because it is also declared in AnglesteelWindow.xaml where the root element is Window . If you want to inherit from another class you have to replace there the Window root by your base class.

public class MyDerivedBaseWindow : Window {}



<ns:MyDerivedBaseWindow >
    <!-- WindowContent-->
</ns:MyDerivedBaseWindow >

But you cannot use a Generic class in XAML. You have to change your logic that the base-window-class that you want to use as window-root is non-generic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM