开发者

How can I make a user control extend a class that extends UserControl?

开发者 https://www.devze.com 2022-12-20 00:29 出处:网络
I want to attempt an MVC design for my little app. I have a normal Csharp class ViewBase which extends UserControl. It\'s a single .cs file.

I want to attempt an MVC design for my little app.

I have a normal Csharp class ViewBase which extends UserControl. It's a single .cs file.

I have multiple classes that I want to extend ViewBase. These are actual UserControls so they have a code behind .cs file and a .xaml file.

However, CSharp tells me that for these classes, their base class "differs from declared in other parts".

Is what I want to do possible at all? What am I doing wrong?

Note that I did not modify my XAML files, so they still use tags.

Here is the relevant code:

// This gives the error in question and ViewBase is underlined
// "Base class of LoginView differs from declared in other parts"
public partial class LoginView : ViewBase {
    public LoginView(Shell shell, ControllerBase controller) : base(shell, controller) {
        InitializeComponent();
    }
}

// This one is a single .cs file
public abstract class ViewBase : UserControl {
 开发者_如何学C   public Shell Shell { get; set; }
    public ControllerBase Controller { get; set; }

    protected ViewBase(Shell shell, ControllerBase controller)
    {
        Shell = shell;
        Controller = controller;
    }
}


Note that I did not modify my XAML files, so they still use tags

That's your problem. You'll need to change:

<UserControl ...>
    ...
</UserControl>

to

<local:ViewBase xmlns:local="clr-namespace:..."
    ...
</local:ViewBase>

The problem is you're telling the compiler you're inheriting ViewBase in one place (the .cs file) and UserControl in another (the .xaml file).

0

精彩评论

暂无评论...
验证码 换一张
取 消