[英]How to use abstract classes inside a UserControl
I have a UserControl
. 我有一个
UserControl
。
I want each UserControl
to Override an Abstract method. 我希望每个
UserControl
可以重写Abstract方法。
This is my abstract class: 这是我的抽象类:
public class MyAbstract
{
public virtual void LoadData()
{
}
}
This my usercontrol with my latest attempt at getting this to work: 这是我的用户控件,也是我最近一次尝试使其工作的尝试:
public partial class ucAbstract : UserControl, MyAbstract
{
public ucAbstract()
{
InitializeComponent();
}
public override void LoadData()
{
base.Load();
{
}
}
}
The error is: 错误是:
Class 'ucAbstract' cannot have multiple base classes: 'UserControl' and 'MyAbstract'
类“ ucAbstract”不能有多个基类:“ UserControl”和“ MyAbstract”
How can I do this? 我怎样才能做到这一点?
ADDITIONAL: I may have to remove this addition and create a new question. 附加:我可能必须删除此添加并创建一个新问题。
This is what I am trying to achieve: 这是我要实现的目标:
My main form contains 2 UserControls
: ucOne
, ucTwo
我的主要形式包含2个
UserControls
: ucOne
, ucTwo
Both of these controls have a method called 'LoadData'. 这两个控件都有一个称为“ LoadData”的方法。
I have a function in my main form: 我有一个主要形式的功能:
void LoadControl(iuserControl myUserControl)
{
myUserControl.LoadData();
}
How about this. 这个怎么样。
Create a Base class
that contains all the common methods of your UserControl
. 创建一个包含所有
UserControl
通用方法的基class
。 Make sure that it is extended with UserControl
class 确保使用
UserControl
类对其进行了扩展
MyAbstract.cs MyAbstract.cs
public abstract class MyAbstract : UserControl
{
public virtual void LoadData()
{
}
}
then create a UserControl
and extend that with MyAbstract
class. 然后创建一个
UserControl
并使用MyAbstract
类进行扩展。 You can use like this. 您可以这样使用。
ucAbstract.xaml.cs ucAbstract.xaml.cs
public partial class ucAbstract : MyAbstract
{
public ucAbstract()
{
InitializeComponent();
}
public override void LoadData()
{
base.LoadData();
{
}
}
}
Also, you need to have <local:MyAbstract>
instead of <UserControl>
in the xaml 另外,您需要在xaml中使用
<local:MyAbstract>
代替<UserControl>
ucAbstract.xaml ucAbstract.xaml
<local:MyAbstract x:Class="YourNamespace.ucAbstract"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:YourNamespace"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<TextBox />
</StackPanel>
</local:MyAbstract>
Note: 注意:
UserControl
s with MyAbstract
as BaseClass. MyAbstract
作为BaseClass创建多个UserControl
。 UserControls
only allow one level of inheritance, at least if the MyAbstract
has a XAML this surely will not work. UserControls
仅允许一个继承级别,至少如果MyAbstract
具有XAML,则这肯定不会起作用。 Reference: Partial declarations, must not specify different base classes 参考: 部分声明,不能指定不同的基类
C# doesn't support multiple inheritance and you cannot inherit from multiple classes ( UserControl
and MyAbstract
). C#不支持多重继承,您不能从多个类(
UserControl
和MyAbstract
)继承。 You could use interface
instead of class like this: 您可以使用
interface
而不是像这样的类:
public interface IMyAbstract
{
void LoadData();
}
Then you should implement the interface like this: 然后,您应该像这样实现接口:
public partial class ucAbstract : UserControl, IMyAbstract
{
public void LoadData()
{
}
}
实际上,您需要具有适当CommandParameter
ICommand
对象来代替LoadData()
方法进行条件加载。
You can Create your UserControl 您可以创建您的UserControl
Public class MyUserControl : UserControl
{
public virtual LoadData()
{
// ...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.