简体   繁体   English

在C#类库项目中使用(合并)资源字典

[英]Using (merged) Resource Dictionary in C# Class Library Project

How can I use a (merged) WPF Resource Dictionary in a C# class library project? 如何在C#类库项目中使用(合并的)WPF资源字典?

Here is what I did: 这是我做的:

In my C# class library project I have a file Dictionary1.xaml like that: 在我的C#类库项目中,我有一个类似的文件Dictionary1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                >
    <Style x:Key="PluginFrameBorderStyle">
    ...
    </Style>
</ResourceDictionary>

Then, I have a UserControl file UserControl1.xaml where I try to use the Dictionary like that: 然后,我有一个UserControl文件UserControl1.xaml ,我尝试使用这样的字典:

<UserControl x:Class="EditorPackageA.BackboneMemberB1Editor.BackboneMemberB1Editor"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         xmlns:local="clr-namespace:EditorPackageA.EditorBase"
         xmlns:prism="http://www.codeplex.com/prism" d:DesignWidth="690.4" d:DesignHeight="460.12">
<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Dictionary1.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
...
</UserControl>

The project compiles but at runtime I get the error: 该项目编译但在运行时我得到错误:

在此输入图像描述

with the exception detail: 除了细节:

在此输入图像描述

The same approach works when applied within a WPF project rather than a Class Library project . WPF项目中应用时,相同的方法也适用,而不是类库项目

What might be the solution here? 这可能是什么解决方案?

Important Addendum: 重要附录:

During design-time I see the effect of the used style that is embedded via the ResourceDictionary, hence the URI of the style and the dictionary must be correct!? 在设计期间,我看到通过ResourceDictionary嵌入的使用样式的效果,因此样式的URI和字典必须正确!?

Try to use so called pack URI. 尝试使用所谓的pack URI。 I think that you have to explicitly specify where the resource dictionary is located. 我认为您必须明确指定资源字典的位置。

<ResourceDictionary Source="pack://application:,,,/TheNameOfClassLibrary;component/Dictionary1.xaml"/>

In the case of a WPF project your approach works because WPF engine by default looks for resource in the assembly being executed (in exe). 对于WPF项目,您的方法可行,因为WPF引擎默认查找正在执行的程序集中的资源(在exe中)。

You probably need to merge your library's resource dictionary in your application resources. 您可能需要在应用程序资源中合并库的资源字典。 You need to edit your App.xaml file and add something like: 您需要编辑App.xaml文件并添加如下内容:

<Application.Resources>
<ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Dictionary1.xaml" />
      </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

You should refer/link the ResourceDictionary xml file to the Source attribute with assembly and component name. 您应该使用程序集和组件名称将ResourceDictionary xml文件引用/链接到Source属性。
Use relative path as follows: 使用相对路径如下:

<ResourceDictionary Source="../Dictionary1.xaml" /> 

If it is not working, then try PACK URL 如果它不起作用,请尝试PACK URL

<ResourceDictionary Source="pack://application:,,,/Your.Base.AssemblyName;component/DictionaryFolder/Dictionary1.xaml" />

Hope it helps you 希望它能帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM