2017-02-26 99 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

很容易錯過... –