如何让我的usercontrol在单独的类库中保存的资源字典中使用样式?

时间:2016-03-30 14:08:17

标签: c# wpf xaml

我有一个名为'Style'的类库,里面有一个名为'Controls.xaml'的xaml文件。 Controls.xaml看起来像这样:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:system="clr-namespace:System;assembly=mscorlib">

    <Style x:Key="DriveImageStyle" TargetType="{x:Type Image}">
        <Setter Property="Width" Value="30" />
        <Setter Property="Height" Value="30" />
        <Setter Property="Margin" Value="2" />
        <Setter Property="HorizontalAlignment" Value="Left" />
    </Style>

    <Style x:Key="DriveLabelStyle" TargetType="{x:Type Label}">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="15" />
        <Setter Property="FontFamily" Value="Calibri Light" />
    </Style>
</ResourceDictionary>

然后我在我的主项目和用户控件中引用了'Styles'我想要使用它我将它添加到参考资料部分:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Styles;component/Controls.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

没有错误显示。但是当我运行应用程序时,我收到错误消息:

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll

Additional information: 'Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '13' and line position '18'.

内部异常:

{"Could not load file or assembly 'Styles, Culture=neutral' or one of its dependencies. The system cannot find the file specified.":"Styles, Culture=neutral"}

有谁能请我帮助我,我看不出我哪里出错了。

1 个答案:

答案 0 :(得分:0)

哇好想通了......这真的很简单,但有时我可以对简单的解决方案蒙蔽......

基本上我需要在实际主应用程序中添加对“样式”的引用。傻我没想到这个。

这是帮助我的线程的链接:

WPF UserControl cannot find XAML resource in referencing project