这听起来很简单,它杀死了我!
我正在尝试将一个用户控件与来自外部程序集的 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 网格中,添加:
<MyControls:SuperControl />
它会编译,但运行应用程序时会得到“无法分配给属性'System.Windows.ResourceDictionary.Source'。[行:10 位置:36]”
我将此添加到应用程序的 App.xaml
<ResourceDictionary Source="/MyControls;component/Styles.xaml" />
同样的错误... :(
有什么想法吗?