簡體   English   中英

使用WPF窗口的通用基類

[英]Using generic base class for WPF window

為什么是這樣:

public abstract class WindowControls<T> : Window

不可能。 我似乎無法弄明白。

    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");
    }
}

我知道泛型如何工作,但不知道為什么不可能讓我的AnglesteelWindow從WindowControls派生。 它給我的錯誤如下:

“解決方案名稱”的基類與其他部分中聲明的不同。

當我看到所謂的其他部分時,它是以下內容:

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

這是在AnglesteelWindow.gics文件中進行的。 如果我從那里刪除它根本沒有任何區別。

添加@MichaelMairegger答案,您可以通過創建另一個繼承自泛型類的非泛型類來達到目標​​,如下所示:

public abstract class WindowControlsOfAngleSteel : WindowControls<AngleSteel>
{

}

並使您的窗口類繼承如下:

來自XAML:

<ns:WindowControlsOfAngleSteel >

</ns:WindowControlsOfAngleSteel >

其中nsWindowControlsOfAngleSteel存在的命名空間。

在代碼中(可選):

public partial class AnglesteelWindow : WindowControlsOfAngleSteel
{

}

您無法更改繼承樹。 AnglesteelWindow是部分的,因為它也在AnglesteelWindow.xaml中聲明,其中根元素是Window 如果你想從另一個類繼承,你必須用你的基類替換Window root。

public class MyDerivedBaseWindow : Window {}



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

但是你不能在XAML中使用Generic類。 您必須更改邏輯,即要用作window-root的基本窗口類是非泛型的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM