简体   繁体   English

使用WPF窗口的通用基类

[英]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. 我知道泛型如何工作,但不知道为什么不可能让我的AnglesteelWindow从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. 这是在AnglesteelWindow.gics文件中进行的。 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: 添加@MichaelMairegger答案,您可以通过创建另一个继承自泛型类的非泛型类来达到目标​​,如下所示:

public abstract class WindowControlsOfAngleSteel : WindowControls<AngleSteel>
{

}

And make your window class inherit from it like this: 并使您的窗口类继承如下:

From XAML: 来自XAML:

<ns:WindowControlsOfAngleSteel >

</ns:WindowControlsOfAngleSteel >

Where ns is the namespace where WindowControlsOfAngleSteel exists. 其中nsWindowControlsOfAngleSteel存在的命名空间。

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 . AnglesteelWindow是部分的,因为它也在AnglesteelWindow.xaml中声明,其中根元素是Window If you want to inherit from another class you have to replace there the Window root by your base class. 如果你想从另一个类继承,你必须用你的基类替换Window root。

public class MyDerivedBaseWindow : Window {}



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

But you cannot use a Generic class in XAML. 但是你不能在XAML中使用Generic类。 You have to change your logic that the base-window-class that you want to use as window-root is non-generic. 您必须更改逻辑,即要用作window-root的基本窗口类是非泛型的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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