繁体   English   中英

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

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

如何在C#类库项目中使用(合并的)WPF资源字典?

这是我做的:

在我的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>

然后,我有一个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>

该项目编译但在运行时我得到错误:

在此输入图像描述

除了细节:

在此输入图像描述

WPF项目中应用时,相同的方法也适用,而不是类库项目

这可能是什么解决方案?

重要附录:

在设计期间,我看到通过ResourceDictionary嵌入的使用样式的效果,因此样式的URI和字典必须正确!?

尝试使用所谓的pack URI。 我认为您必须明确指定资源字典的位置。

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

对于WPF项目,您的方法可行,因为WPF引擎默认查找正在执行的程序集中的资源(在exe中)。

您可能需要在应用程序资源中合并库的资源字典。 您需要编辑App.xaml文件并添加如下内容:

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

您应该使用程序集和组件名称将ResourceDictionary xml文件引用/链接到Source属性。
使用相对路径如下:

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

如果它不起作用,请尝试PACK URL

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

希望它能帮到你

暂无
暂无

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

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