C# 为WPF窗口使用泛型基类
为什么会这样:C# 为WPF窗口使用泛型基类,c#,wpf,generics,C#,Wpf,Generics,为什么会这样: public abstract class WindowControls<T> : Window 公共抽象类窗口控件:窗口 不可能。我似乎不明白 public partial class AnglesteelWindow : WindowControls<AngleSteel> { private UCListView uc; public AnglesteelWindow() { InitializeComp
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");
}
}
公共部分类:窗口控件{
私有UCListView uc;
公共窗(){
初始化组件();
uc=新的UCListView();
uc.SubmitClick+=新事件处理程序(按钮按下);
this.uc.grid.PreviewMouseLeftButtonUp+=
新System.Windows.Input.MouseButtonEventHandler(
这是我的鼠标垫14);
stkTest.Children.Add(uc);
uc.amountLabel.Content=“毫米”;
uc.grid.ItemsSource=数据库逻辑.MaterialTable(“角钢”).DefaultView;
基础材料(uc,“角钢”);
}
}
我知道泛型是如何工作的,但不知道为什么不能使我的AnglesteelWindow从WindowControls派生出来。
它给我的错误如下:
public partial class AnglesteelWindow :
WindowControls<AngleSteel> System.Windows.Markup.IComponentConnector {
“解决方案名称”的基类与其他部分中声明的基类不同
当我看到所谓的其他部分时,它是以下内容:
public partial class AnglesteelWindow :
WindowControls<AngleSteel> System.Windows.Markup.IComponentConnector {
公共部分类窗口:
WindowControls System.Windows.Markup.IComponentConnector{
这是在AnglesteelWindow.g.i.cs文件中进行的。如果我将其从那里删除,则没有任何区别。您无法更改继承树。
AnglesteelWindow
是部分的,因为它也在AnglesteelWindow.xaml中声明,其中根元素是Window
。如果您想从另一个类继承,则没有o将窗口
根目录替换为基类
public class MyDerivedBaseWindow : Window {}
<ns:MyDerivedBaseWindow >
<!-- WindowContent-->
</ns:MyDerivedBaseWindow >
公共类MyDerivedBaseWindow:Window{}
但不能在XAML中使用泛型类。必须更改逻辑,使要用作窗口根的基本窗口类是非泛型的。添加@MichaelMairegger answer,您可以通过创建从泛型类继承的另一个非泛型类来达到目标,如下所示:
public abstract class WindowControlsOfAngleSteel : WindowControls<AngleSteel>
{
}
这个窗口有XAML文件吗?问题不在于你的C#代码,而在于它与XAML文件不兼容。如果你删除基类中的
abstract
,那么答案是正确的。@MichaelMairegger,你为什么认为abstract
需要删除?我实际上测试了这段代码。对不起,我错了,我想我是将抽象类作为根类会导致错误。我从未尝试过。问题是,这将用于6个窗口,每个窗口的名称不同,但功能相同,因此我将制作6个WindowControlsOfAnglesteel,这似乎有点多:它们都需要从WindowControls
或其他WindowControls继承吗代码>?
public partial class AnglesteelWindow : WindowControlsOfAngleSteel
{
}