0
听起来很简单,它让我难过!从外部程序集中使用带有静态资源的usercontrols
我想在一个外部程序集的ResourceDictionary中使用一个用户控件,但我在运行时遇到异常。
下面是如何重现:
- 创建Silverlight类库称为MyControls.dll
创建一个名为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>
创建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>
创建Silverlight应用程序调用SL并添加Mycontrols作为参考
在MainPage.xaml中网格,添加:
<MyControls:SuperControl />
这将编译,但在运行应用程序“无法分配给属性'System.Windows.ResourceDictionary.Source'。 [行:10职位:36]?”
我已将此添加到应用程序的App.xaml
<ResourceDictionary Source="/MyControls;component/Styles.xaml" />
同样的错误... :(
有什么想法
我有类似的问题和原因是在引用文件时使用反斜杠(\)而不是正斜杠(/)。 VS中的xaml解析器能够解析位置,但运行时正在生成错误。希望这会帮助别人。 – 2011-08-09 18:27:09