C# 将usercontrols与外部程序集的静态资源一起使用

C# 将usercontrols与外部程序集的静态资源一起使用,c#,silverlight-4.0,staticresource,C#,Silverlight 4.0,Staticresource,听起来很简单,简直要了我的命 我试图将一个usercontrol与外部程序集的ResourceDictionary中的样式一起使用,但在运行时出现异常 以下是如何复制: 创建名为MyControls.dll的silverlight类库 创建一个名为超级控件的用户控件: <UserControl.Resources> <ResourceDictionary Source="MyControls;component/Styles.xaml" x:Key="Styles" /

听起来很简单,简直要了我的命

我试图将一个usercontrol与外部程序集的ResourceDictionary中的样式一起使用,但在运行时出现异常

以下是如何复制:

创建名为MyControls.dll的silverlight类库 创建一个名为超级控件的用户控件:

<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应用程序并添加MyControl作为参考

在MainPage.xaml网格中,添加:

<MyControls:SuperControl />
它将进行编译,但运行获取的应用程序时未能将其分配给属性“System.Windows.ResourceDictionary.Source”。[行:10位置:36]

我将其添加到应用程序的App.xaml中

<ResourceDictionary Source="/MyControls;component/Styles.xaml" />
同样的错误…:

有什么想法吗

第2步。 你忘了

来源=MyControls;component/Styles.xaml

Source=/MyControls;component/Styles.xaml


我遇到了类似的问题,原因是在引用文件时使用了反斜杠而不是正斜杠。VS中的xaml解析器能够解析该位置,但在运行时生成了一个错误。希望这能帮助其他人。