2011-06-04 15 views
2

我按照教程做了一個自定義控件。我基本上做的是做一個新項目,添加一個文件CategoryBar.cs和一個名爲Themes的文件夾,文件Themes\generic.xaml(編譯類型設置爲'resource')。然後我寫了一個類CategoryBar.cs,用ResourceDictionary填充generic.xaml。我們稱這個項目爲'UILib':在WP7中製作多個自定義控件Silverlight給出奇怪的例外

<?xml version="1.0" encoding="utf-8" ?> 
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" 
       xmlns:local="clr-namespace:ErnestUILib"> 
    <Style TargetType="local:CategoryBar"> 
     <Setter Property="Background" Value="Black" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="local:CategoryBar"> 
         <Grid x:Name="GridView" Background="{TemplateBinding Background}" Margin="0,0,0,8"> 
          <!-- The grid rowdefs, coldefs and whatever makes up the grid --> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

而且它在我添加對該庫的引用的項目中運行得很好。我添加了屬性xmlns:EULib="clr-namespace:UILib;assembly=UILib"<phone:PhoneApplicationPage .. />,它工作正常。現在,我想實現另一個控件(因爲我想爲自定義UI控件擁有一個獨立的庫)。所以,現在我的generic.xaml的樣子:

<?xml version="1.0" encoding="utf-8" ?> 
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" 
       xmlns:local="clr-namespace:ErnestUILib"> 
    <!-- THE NEW CUSTOM CONTROL --> 
    <Style TargetType="local:PaginationBar"> 
     <Setter Property="Background" Value="Black" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="local:PaginationBar"> 
         <Grid x:Name="GridView" Background="{TemplateBinding Background}" Margin="0,0,0,8"> 
          <!-- The grid rowdefs, coldefs and whatever makes up the grid --> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 


    <!-- THE PREVIOUS CUSTOM CONTROL --> 
    <Style TargetType="local:CategoryBar"> 
     <Setter Property="Background" Value="Black" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="local:CategoryBar"> 
         <Grid x:Name="GridView" Background="{TemplateBinding Background}" Margin="0,0,0,8"> 
          <!-- The grid rowdefs, coldefs and whatever makes up the grid --> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

在這裏,我在PaginationBar.cs創建的類PaginationBar和它的所有設置,但是當我嘗試在我的應用程序的XAML文件中使用它,它顯示了一個白色 - 在設計器視圖中填充矩形,在其左上角有一個十字形,它表示異常是由'Control_TargetTypeMismatch'引起的。經過我的一些技巧後,沒有任何工作,但設計器只是不加載,當我使用<UILib:PaginationBar .. />,而是提供了一個錯誤System.Reflection.TargetInvocationException(異常已被調用的目標引發)。當我運行該項目時,它會給出一些XamlParseException錯誤。這是我能夠從中獲得一些細節的唯一例外,我認爲這些細節甚至都沒有什麼用處。無論如何,這是我得到的XamlParseException:XamlParseExceptionDetails

我不知道如何繼續。任何幫助是極大的讚賞。感謝預期:)

回答

1

驗證PaginationBar是在相同的命名空間中定義的:「clr-namespace:ErnestUILib」。此外,還應確保您已在控制的構造函數中設置正確的DefaultStyleKey:

public PaginationBar() 
    { 
     DefaultStyleKey = typeof(PaginationBar); 
    } 
+0

實際上是由於一些問題,我輸入的文本或上帝知道,這個問題沒有正確顯示的方式。我實際上已經提出了兩個不同的代碼集。在第二部分中,我確實提到了TargetType爲「local:PaginationBar」。非常感謝您的回覆,但是現在我可以重新閱讀我的問題了,因爲我已經正確編輯了它嗎?我很抱歉:) – 2011-06-04 07:33:40

+0

非常感謝你!原來,因爲我正在複製粘貼上一次控制的代碼,所以我忘了更改這部分。 P.S:我已經將這兩個控件都放在了你作爲聯合創始人的博客的教程之後。幹得好!乾杯:) – 2011-06-06 04:44:48