【发布时间】:2010-11-10 20:45:43
【问题描述】:
这听起来很简单,我要死了!
我正在尝试将一个用户控件与来自外部程序集的 ResourceDictionary 的样式一起使用,但在运行时出现异常。
复制方法如下:
- 创建一个名为 MyControls.dll 的 silverlight 类库
-
创建一个名为 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> 创建一个名为 SL 的 Silverlight 应用程序并添加 Mycontrols 作为参考
-
在 MainPage.xaml Grid 中,添加:
<MyControls:SuperControl /> 它会编译,但在运行应用程序时会出现“无法分配给属性 'System.Windows.ResourceDictionary.Source'。[行:10 位置:36]”
-
我将此添加到应用程序的 App.xaml
<ResourceDictionary Source="/MyControls;component/Styles.xaml" /> 同样的错误... :(
有什么想法吗?
【问题讨论】:
-
我遇到了类似的问题,原因是在引用文件时使用了反斜杠 () 而不是正斜杠 (/)。 VS 中的 xaml 解析器能够解析该位置,但在运行时会生成错误。希望这会帮助其他人。
标签: c# silverlight-4.0 staticresource