使用 Visual Studio 设计器时继承 UserControl 基础 class 时出错
Error inheriting UserControl base class while working with Visual Studio designer
嗯,标题其实应该很好地说明了问题。假设我创建了一个新的 UserControl
,但是在我的应用程序中,许多这些元素共享一个公共代码,并且它们是 "subgroup" 用户控件的一部分。
合乎逻辑的是在生成的CustomUserControl
和UserControl
之间"inject"一个class;
类似于:
public abstract class CommonCustomControl : UserControl
{
public CommonCustomControl(int v, string other) {
}
}
public partial class CustomUserControl : CommonCustomControl
{
CustomUserControl(int v, string other) : base(v, other) {
}
}
现在的问题是,class 只是 "partially" 由 visual studio 生成的。所以改变生成的 CustomUserControl
class 给出一个错误:
"Base class differs from declared in other parts"
我怎样才能避免这个错误?虽然仍然能够在 visual studio 的图形用户界面设计器中实际设计我的用户控件元素?
我已经尝试过this question提供的答案。但它似乎根本不起作用。 (也许是因为那次讨论的是 winforms 而不是 WPF 版本)
[TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider<InterfaceHandler, UserControl>))]
public abstract class CommonCustomControl : UserControl
{
private readonly int _v;
private readonly string _other;
public CommonCustomControl(int v, string other) {
_v = v;
_other = other;
}
}
public partial class CustomUserControl : CommonCustomControl
{
public CustomUserControl(int v, string other) : base(v, other) {
}
}
出了什么问题?
此处只显示了您的 C# 代码,但我会赌一下这可能是什么。
您声明的 UserControl
继承自基础 class。
UserControl
也是一个 partial class
.
部分 classes 通常意味着此 class 别处还有其他代码。
错误表明两个部分 class 之间的基数 class 不同。
如果你没有自己的第二个 partial class
这个 UserControl
(即它不仅仅是一个 class,而是一个实际的用户控件),那么我假设剩下的的定义将在 XAML
.
取决于您如何实施控件:
你可能有这个:
<UserControl x:Class="NamespaceHere.CustomUserControl"> ... </UserControl>
它应该是这样的:
<CommonCustomControl x:Class="NamespaceHere.CustomUserControl"> ... </CommonCustomControl>
如果没有,你能不能也展示一下你的 XAML
?
嗯,标题其实应该很好地说明了问题。假设我创建了一个新的 UserControl
,但是在我的应用程序中,许多这些元素共享一个公共代码,并且它们是 "subgroup" 用户控件的一部分。
合乎逻辑的是在生成的CustomUserControl
和UserControl
之间"inject"一个class;
类似于:
public abstract class CommonCustomControl : UserControl
{
public CommonCustomControl(int v, string other) {
}
}
public partial class CustomUserControl : CommonCustomControl
{
CustomUserControl(int v, string other) : base(v, other) {
}
}
现在的问题是,class 只是 "partially" 由 visual studio 生成的。所以改变生成的 CustomUserControl
class 给出一个错误:
"Base class differs from declared in other parts"
我怎样才能避免这个错误?虽然仍然能够在 visual studio 的图形用户界面设计器中实际设计我的用户控件元素?
我已经尝试过this question提供的答案。但它似乎根本不起作用。 (也许是因为那次讨论的是 winforms 而不是 WPF 版本)
[TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider<InterfaceHandler, UserControl>))]
public abstract class CommonCustomControl : UserControl
{
private readonly int _v;
private readonly string _other;
public CommonCustomControl(int v, string other) {
_v = v;
_other = other;
}
}
public partial class CustomUserControl : CommonCustomControl
{
public CustomUserControl(int v, string other) : base(v, other) {
}
}
出了什么问题?
此处只显示了您的 C# 代码,但我会赌一下这可能是什么。
您声明的 UserControl
继承自基础 class。
UserControl
也是一个 partial class
.
部分 classes 通常意味着此 class 别处还有其他代码。
错误表明两个部分 class 之间的基数 class 不同。
如果你没有自己的第二个 partial class
这个 UserControl
(即它不仅仅是一个 class,而是一个实际的用户控件),那么我假设剩下的的定义将在 XAML
.
取决于您如何实施控件:
你可能有这个:
<UserControl x:Class="NamespaceHere.CustomUserControl"> ... </UserControl>
它应该是这样的:
<CommonCustomControl x:Class="NamespaceHere.CustomUserControl"> ... </CommonCustomControl>
如果没有,你能不能也展示一下你的 XAML
?