gpt4 book ai didi

c# - 如何使用户控件扩展一个扩展 UserControl 的类?

转载 作者:可可西里 更新时间:2023-11-01 08:56:07 26 4
gpt4 key购买 nike

我想为我的小应用尝试 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;
}
}

最佳答案

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

那是你的问题。您需要更改:

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

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

问题是您要告诉编译器您在一个地方(.cs 文件)继承了 ViewBase 并在其中继承了 UserControl另一个(.xaml 文件)。

关于c# - 如何使用户控件扩展一个扩展 UserControl 的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2265320/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com