【问题标题】:Error inheriting UserControl base class while working with Visual Studio designer使用 Visual Studio 设计器时继承 UserControl 基类时出错
【发布时间】:2017-02-27 09:23:31
【问题描述】:

好吧,标题实际上应该很好地解释了这个问题。假设我创建了一个新的UserControl,但是在我的应用程序中,许多元素共享一个公共代码,并且它们是用户控件“子组”的一部分。

合乎逻辑的事情是在生成的CustomUserControlUserControl之间“注入”一个类;

类似于:

public abstract class CommonCustomControl : UserControl 
{ 
    public CommonCustomControl(int v, string other) {
    }
}
public partial class CustomUserControl : CommonCustomControl
{ 
    CustomUserControl(int v, string other) : base(v, other) {
    }
}

现在的问题是,该类仅“部分”由 Visual Studio 生成。所以更改生成的CustomUserControl 类会报错:
“基类与其他部分声明的不同”

如何防止此错误?虽然仍然能够在 Visual Studio 的 gui 设计器中实际设计我的用户控件元素?

我已经尝试过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# wpf visual-studio inheritance user-controls


    【解决方案1】:

    您在这里只能看到您的 C# 代码,但我会试一试这可能是什么。

    您声明的UserControl 继承自基类。 UserControl 也是 partial class

    部分类通常意味着该类在别处还有其他代码。

    错误说明两个部分类之间的基类不同。

    如果您没有自己的第二个 partial class 用于此 UserControl(即它不仅仅是一个类,而是一个实际的用户控件),那么我假设其余定义将在 XAML .

    取决于您实施控制的方式:

    你可能有这个:

    <UserControl x:Class="NamespaceHere.CustomUserControl"> ... </UserControl>
    

    应该是这样的:

    <CommonCustomControl x:Class="NamespaceHere.CustomUserControl"> ... </CommonCustomControl>
    

    如果没有,您能否也显示您的XAML

    【讨论】:

    • 啊这样的事情我也能做。
    猜你喜欢
    • 2016-11-10
    • 2012-07-17
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多