2010-11-10 33 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”