首页 > 解决方案 > 从外部项目引用资源字典

问题描述

是否可以在单独的库(框架)项目中制作资源字典,然后在另一个库项目中引用它?在这个单独的项目中,我有一个想要使用它的 UserControl。

这就是我的解决方案的设置方式。

在此处输入图像描述

SharedProject 有资源字典,我在我的插件项目中引用它。然后我将我的插件库导出到 MainProject 并让它显示我的插件视图。

我有所有这些工作......除了我的字典。我尝试在每个视图中引用我的字典

<UserControl.Resources>  
        <ResourceDictionary x:Key="dictionary">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/InternalShared;component/Resources/Dictionary1.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/InternalShared;component/Resources/Dictionary2.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

但是当我尝试引用现有样式时...

<Button     Command="{Binding CommandOpenFile}"
            Style="{DynamicResource GenericButtonStyle}" 
            IsEnabled="{Binding IsEnabled}"
            Content="Restore Window" 
            Height="Auto" />

但我得到资源“GenericButtonStyle”无法解决。这种情况在我尝试引用任何字典中的样式的任何地方都会发生。我不知道为什么。

谁能告诉我为什么会这样?我还需要在其他地方设置此声明吗?

标签: wpfxamlresourcedictionary

解决方案


我维护一个由许多不同解决方案使用的外部 WPF UI 库。

  • 确保 InternalShared 是 WPF DLL
  • 将您的外部资源合并到App.xaml您的主 WPF 应用程序中

例如,使用我的资源的应用程序将它们合并为这样:

<ResourceDictionary Source="/InternalShared;component/Resources/Dictionary1.xaml"/>

推荐阅读