2017-02-26 63 views
0

那么标题应该很好地解释问题。考虑我创建一个新的UserControl,但是在我的应用程序中,许多这些元素共享一个通用代码,并且它们是用户控件“子组”的一部分。使用Visual Studio设计器时继承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) { 
    } 
} 

出了什么问题?

回答

0

你只有你的C#代码在这里查看,但我会踢这可能是什么。

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

部分类通常意味着其他地方还有其他代码。

错误是指出两个部分类之间的基类是不同的。

如果您对UserControl没有自己的第二个partial class(即它不只是一个类,而是一个实际的用户控件),那么我认为其余的定义将在XAML中。根据你如何实现你的控制

你可能有这样的:

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

它应该是这样的:

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

如果没有,你可以展示你XAML太?

+0

啊我也可以做这样的事情。 – paul23

+0

很容易错过... –