2017-08-16 69 views
0

在WPF應用程序(VB.net)中,我試圖創建一個基本用戶控件,其中2個其他用戶控件從中繼承。如何爲多個用戶控件實現超類

這是我到現在爲止:

我的基類:

PUblic MustInherit Class BaseUserControl 
    Inherits UserControl 
    Implements INotifyPropertyChanged 

    'common properties 
    'common methods 

End Class 

我繼承的用戶控件,.xaml.vb文件:

PUblic Class myUserControlOne 
    Inherits BaseUserControl 

    Public Sub New() 
     InitializeComponent() 
    End Sub 
End Class 

我繼承的使用控件,.xaml文件:

<local:BaseUserControl x:Class="myUserControlOne" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:TestProject" 
    mc:Ignorable="d" 
    d:DesignHeight="300" 
    d:DesignWidth="300"> 


    <Grid> 
    </Grid> 

</local:BaseUserControl> 

我看到幾篇文章和問題上堆棧溢出,像thisthisthisthis等。但是,我仍然得到一個錯誤,我沒有得到我的地方錯誤的根源。

這是我目前的錯誤:命名空間「clr-namespace:TestProject」中不存在名稱「BaseUserControl」。

我對WPF和VB.net比較陌生,我很感激任何幫助。

+0

是在WPF應用程序項目中定義的BaseUserControl類還是您有幾個不同的項目?另外,什麼是? – mm8

+0

一切都在一個單一的項目。這是一個錯誤的問題,感謝您指出它;) – Yvonnila

+0

而且你不能建立該項目? – mm8

回答

-1

這樣的事情?

基類

public class BaseClass : uielement 
    { 
     static BaseClass() 
     { 
      DefaultStyleKeyProperty.OverrideMetadata(
       typeof(BaseClass), new FrameworkPropertyMetadata(typeof(Baseclass))); 
     } 

childClass

public class ChildClass : BaseClass 
    { 
     static ChildClass() 
     { 
      DefaultStyleKeyProperty.OverrideMetadata(
       typeof(ChildClass), new FrameworkPropertyMetadata(typeof(ChildClass))); 
     } 

,如果你不覆蓋在你的子類的defaultstylekeyproperty他們會用父母的風格。

只要在那裏定義正常的構造函數,靜態構造函數總是先自動調用,然後再調用正常的構造函數。

然後某處定義你的風格是這樣的:

<Style TargetType="{x:Type test:BaseClass}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="test:BaseClass"> 
        <Path Data="M-5,-3 -5,2 l10, 10 l-10,10 v5" Fill="LightBlue"/> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

我曾經有過同樣的問題, 編輯:錯過了VB.net,在VB.net從用戶控件擴展,而不是一個的UIElement

+0

沒有給予理由downvote – Lloyd

+1

我沒有downvote,無論如何,謝謝你的回覆。但是我沒有看到與我想問的關係。 – Yvonnila

+0

別擔心,我知道你沒有。這不會改變公衆的分數。 如果你只是想解決你的錯誤,這是一個intellisense錯誤,這是很常見的。你可以切換到x86重建,回到x64並重建,它也可以修復它。 這是一個關於如何解決「SomeBaseControl」不能作爲XAML文件的根的示例,因爲它在嘗試獲取usercontrol繼承工作時是使用XAML錯誤定義的。如果你發現了另一種繼承usercontrol的方法,那也沒關係。 – Lloyd

相關問題