C# WPF 中 DLL 的资源字典

Resource Dictionary for DLL in C# WPF

有没有办法为一个DLL加载一次资源字典并在所有控件中使用它?我尝试在仅包含资源字典的 DLL 中创建一个应用程序。不出所料,这不起作用。

解决构建操作的问题后,创建多个应用程序时抛出异常。

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Application.Resources>
    <ResourceDictionary Source="pack://application:,,,/HelperWPF;component/Display/DefaultDictionary.xaml"/>
  </Application.Resources>
</Application>

您需要 MergedDictionaries 标签,即使它只是一个 ResourceDictionary

<ResourceDictionary >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/App.Resources;component/Styles/Brushes.xaml" />
         <ResourceDictionary Source="pack://application:,,,/App.Resources;component/Styles/Fonts.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

我还发现,如果您在资源程序集中使用合并字典,它就不起作用。您需要分别参考每个

我可以通过使以下静态 class:

使用相同的资源字典实例
internal static class LoadResourceDictionary
{
    static LoadResourceDictionary()
    {
        resourceDictionary = Application.LoadComponent(new Uri("/DllName;component/Shared/ReusedDictionary.xaml",UriKind.RelativeOrAbsolute)) as ResourceDictionary;
    }

    public static ResourceDictionary resourceDictionary;
}

然后在 class

中引用它
public SingleAlarm_UC()
{
    this.Resources = LoadResourceDictionary.resourceDictionary;
    InitializeComponent();
}