【问题标题】:How can I make a user control extend a class that extends UserControl?如何使用户控件扩展扩展 UserControl 的类?
【发布时间】:2010-02-15 10:42:53
【问题描述】:

我想为我的小应用尝试 MVC 设计。

我有一个扩展 UserControl 的普通 Csharp 类 ViewBase。这是一个 .cs 文件。

我有多个要扩展 ViewBase 的类。这些是实际的用户控件,因此它们在 .cs 文件和 .xaml 文件后面有一个代码。

但是,CSharp 告诉我,对于这些类,它们的基类“与其他部分中声明的不同”。

我想做的事有可能吗?我做错了什么?

请注意,我没有修改我的 XAML 文件,因此它们仍然使用标签。

以下是相关代码:

// 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 {
    public Shell Shell { get; set; }
    public ControllerBase Controller { get; set; }

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

【问题讨论】:

    标签: c# wpf inheritance


    【解决方案1】:

    请注意,我没有修改我的 XAML 文件,所以他们仍然使用标签

    那是你的问题。你需要改变:

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

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

    问题是你告诉编译器你在一个地方继承ViewBase.cs文件)和UserControl在另一个地方(.xaml em> 文件)。

    【讨论】:

    • 这需要详细说明
    猜你喜欢
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    相关资源
    最近更新 更多