【问题标题】:Using usercontrols with static resources from external assembly将用户控件与来自外部程序集的静态资源一起使用
【发布时间】:2010-11-10 20:45:43
【问题描述】:

这听起来很简单,我要死了!

我正在尝试将一个用户控件与来自外部程序集的 ResourceDictionary 的样式一起使用,但在运行时出现异常。

复制方法如下:

  1. 创建一个名为 MyControls.dll 的 silverlight 类库
  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. 创建一个名为 SL 的 Silverlight 应用程序并添加 Mycontrols 作为参考

  5. 在 MainPage.xaml Grid 中,添加:

    <MyControls:SuperControl />
    
  6. 它会编译,但在运行应用程序时会出现“无法分配给属性 'System.Windows.ResourceDictionary.Source'。[行:10 位置:36]”

  7. 我将此添加到应用程序的 App.xaml

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

有什么想法吗?

【问题讨论】:

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

标签: c# silverlight-4.0 staticresource


【解决方案1】:

第 2 步。 你忘了 /。

Source="MyControls;component/Styles.xaml"

Source="/MyControls;component/Styles.xaml"

【讨论】:

    猜你喜欢
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多