在WPF中使用基类
我对WPF中的基类有问题。我试图用一些基本元素创建一个基类,以便其他窗口可以继承这些组件。但当我继承基类时,我所拥有的只是一个空窗口,没有这些元素。为了更好地理解,我将代码放在这里:在WPF中使用基类,wpf,class,base,Wpf,Class,Base,我对WPF中的基类有问题。我试图用一些基本元素创建一个基类,以便其他窗口可以继承这些组件。但当我继承基类时,我所拥有的只是一个空窗口,没有这些元素。为了更好地理解,我将代码放在这里: using XSoftArt.WPFengine; namespace XSoftArt { public class WindowBase : Window { public WindowBase() { } } Windows的代码,whitch继承了Windo
using XSoftArt.WPFengine;
namespace XSoftArt
{
public class WindowBase : Window
{
public WindowBase()
{
}
}
Windows的代码,whitch继承了WindowBase:
namespace XSoftArt.WPFengine
{
public partial class NewAbility : WindowBase
{
public NewAbility()
{
base.ChildForm = this; InitializeComponent();
}
}
}
或者也许有人可以在wpf中提供一个工作示例或与实现基类的链接?
谢谢我不认为我会像您尝试使用继承的方式那样在WPF中使用它 我试着回答你的问题。如果我理解正确的话,你在尝试这样的事情:
您可以尝试将“基本”UI元素组织到WPF用户控件中。也许能指引你走向正确的方向。祝你好运 我不认为我会像您尝试使用它那样在WPF中使用继承 我试着回答你的问题。如果我理解正确的话,你在尝试这样的事情:
您可以尝试将“基本”UI元素组织到WPF用户控件中。也许能指引你走向正确的方向。祝你好运 我不认为你真的需要做你正在做的事情,但是这是可行的。我认为您只是忘记了调用基类构造函数
using XSoftArt.WPFengine;
namespace XSoftArt
{
public class WindowBase : Window
{
//call base ctor
public WindowBase() : base()
{
}
}
}
您还需要从继承的类执行此操作:
namespace XSoftArt.WPFengine
{
public partial class NewAbility : WindowBase
{
public NewAbility() : base()
{
base.ChildForm = this; InitializeComponent();
}
}
}
如果您还有一个XAML定义的视图,则需要确保您的视图是WindowBase。要执行此操作,请更改以下内容:
<Window x:Class="MyApp.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
...
>
<Grid>
</Grid>
</Window>
为此:
<local:WindowBase x:Class="MyApp.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:XSoftArt;"
...
>
<Grid>
</Grid>
</local:WindowBase>
如果您在Reflector中查看这个类,您将看到构造函数调用Window类自己的“Initialize()”方法,该方法设置了许多动态的东西。具体来说,它似乎将自己连接到调度程序,调度程序是所有UI事件的工作队列。我认为您确实不需要做您正在做的事情,但它是可行的。我认为您只是忘记了调用基类构造函数
using XSoftArt.WPFengine;
namespace XSoftArt
{
public class WindowBase : Window
{
//call base ctor
public WindowBase() : base()
{
}
}
}
您还需要从继承的类执行此操作:
namespace XSoftArt.WPFengine
{
public partial class NewAbility : WindowBase
{
public NewAbility() : base()
{
base.ChildForm = this; InitializeComponent();
}
}
}
如果您还有一个XAML定义的视图,则需要确保您的视图是WindowBase。要执行此操作,请更改以下内容:
<Window x:Class="MyApp.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
...
>
<Grid>
</Grid>
</Window>
为此:
<local:WindowBase x:Class="MyApp.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:XSoftArt;"
...
>
<Grid>
</Grid>
</local:WindowBase>
如果您在Reflector中查看这个类,您将看到构造函数调用Window类自己的“Initialize()”方法,该方法设置了许多动态的东西。具体地说,它似乎将自己连接到了Dispatcher,Dispatcher是所有UI事件的工作队列。特别是,您希望确保调用基类的InitializeComponent()方法-这是创建在XAML中定义的控件的函数
如果要继承控件和行为,则派生类是很好的,但是考虑使用更灵活的方法来管理一组公共控件。调用基类的方法-这是创建在XAML中定义的控件的函数
如果要继承控件和行为,那么派生类是很棒的,但是考虑使用更灵活的方式来管理一组共同的控件。
什么让你说它根本上是错误的,是不可能的?看起来像是一种飞跃,你说呢?@Anderson,这取决于我是否理解OP的问题。如果我这样做了,他说的是通过从另一个控件的类继承从XAML文件到XAML文件的继承,那么不,这是不可能的。XAML就是不能那样工作。如果我误解了他,那么也许有办法,但根据OP的描述,我严重怀疑在WPF中走这条路是否明智。不过,也许我错了。“我编辑了我的答案来反映这一点。”@Anderson,谢谢你叫我出来:-)看起来我错了。我仍然强烈建议不要这样做,并建议使用UserControl路径,但如果可能的话,那也是可能的!是什么让你说这根本是错误的,是不可能的?看起来像是一种飞跃,你说呢?@Anderson,这取决于我是否理解OP的问题。如果我这样做了,他说的是通过从另一个控件的类继承从XAML文件到XAML文件的继承,那么不,这是不可能的。XAML就是不能那样工作。如果我误解了他,那么也许有办法,但我严重怀疑在WPF中这样做是否明智