2010-11-10 27 views
0

聽起來很簡單,它讓我難過!從外部程序集中使用帶有靜態資源的usercontrols

我想在一個外部程序集的ResourceDictionary中使用一個用戶控件,但我在運行時遇到異常。

下面是如何重現:

  1. 創建Silverlight類庫稱爲MyControls.dll
  2. 創建一個名爲SuperControl的用戶控件:

    <UserControl.Resources> 
        <ResourceDictionary Source="MyControls;component/Styles.xaml" x:Key="Styles" /> 
    </UserControl.Resources> 
    
    
    <Grid x:Name="LayoutRoot" Background="White"> 
        <TextBlock Style="{StaticResource MyStyle}" Text="Hello"/> 
    </Grid> 
    
  3. 創建Styles.xaml ResourceDictionary中和加:

    <Style x:Key="MyStyle" TargetType="TextBlock"> 
        <Setter Property="FontSize" Value="15"/> 
        <Setter Property="FontWeight" Value="Bold"/> 
        <Setter Property="TextWrapping" Value="Wrap"/> 
        <Setter Property="Margin" Value="0,15,0,4"/> 
        <Setter Property="HorizontalAlignment" Value="Left"/> 
    </Style> 
    

  4. 創建Silverlight應用程序調用SL並添加Mycontrols作爲參考

  5. 在MainPage.xaml中網格,添加:

    <MyControls:SuperControl /> 
    
  6. 這將編譯,但在運行應用程序「無法分配給屬性'System.Windows.ResourceDictionary.Source'。 [行:10職位:36]?」

  7. 我已將此添加到應用程序的App.xaml

    <ResourceDictionary Source="/MyControls;component/Styles.xaml" /> 
    
  8. 同樣的錯誤... :(

有什麼想法

+0

我有類似的問題和原因是在引用文件時使用反斜槓(\)而不是正斜槓(/)。 VS中的xaml解析器能夠解析位置,但運行時正在生成錯誤。希望這會幫助別人。 – 2011-08-09 18:27:09

回答

0

第2步。 You forgot /。

源= 「MyControls;組件/ Styles.xaml」

源= 「/ MyControls;組件/ Styles.xaml」