简体   繁体   中英

How to use abstract classes inside a UserControl

I have a UserControl .

I want each UserControl to Override an Abstract method.

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'

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

Both of these controls have a method called '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 . Make sure that it is extended with UserControl class

MyAbstract.cs

public abstract class MyAbstract   : UserControl
{
    public virtual void LoadData()
    {

    }
}

then create a UserControl and extend that with MyAbstract class. You can use like this.

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

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:

  • You can create multiple UserControl s with MyAbstract as BaseClass.
  • UserControls only allow one level of inheritance, at least if the MyAbstract has a XAML this surely will not work.

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 ). You could use interface instead of class like this:

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

Public class MyUserControl : UserControl
{
public virtual LoadData()
{
// ...
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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