wpf 引用另一个程序集中的 ResourceDictionary

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31716752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:18:36  来源:igfitidea点击:

Referencing a ResourceDictionary in another assembly

c#wpfxaml

提问by Pinx0

I have a class library project that is common to several other projects. It's assembly name is "MyCompany" and its default namespace also "".

我有一个类库项目,与其他几个项目通用。它的程序集名称是“MyCompany”,它的默认命名空间也是“”。

Then in one of these other projects (namespace "MyCompany.Something") I have the dll referenced, and I want to use a resource dictionary that I have in "MyCompany".

然后在这些其他项目之一(命名空间“MyCompany.Something”)中,我引用了 dll,并且我想使用“MyCompany”中的资源字典。

I found this: Add ResourceDictionary to class library

我发现了这个:Add ResourceDictionary to class library

So I did exactly what it says, my xaml file located at "MyCompany" root directory is named "Recursos.xaml", built action set to Resource, not copy and blank custom tool and custom tool namespace, with the following content:

所以我完全按照它所说的去做,我位于“MyCompany”根目录的 xaml 文件被命名为“Recursos.xaml”,构建操作设置为资源,而不是复制和空白自定义工具和自定义工具命名空间,具有以下内容:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
    <ResourceDictionary Source="pack://application:,,,/Resources/Icons.xaml"/>
 </ResourceDictionary.MergedDictionaries>
  -- some other styles...
 </ResourceDictionary>

Then, in my WPF app project, I have in App.xaml the following:

然后,在我的 WPF 应用程序项目中,我在 App.xaml 中有以下内容:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MyCompany;component/Recursos.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Just added there the Controls.xaml for testing purposes. That one works, but mine doesn't:

刚刚添加了 Controls.xaml 用于测试目的。那个有效,但我的不行:

An error occurred while finding the resource dictionary "pack://application:,,,/MyCompany;component/Recursos.xaml"

查找资源字典时出错 "pack://application:,,,/MyCompany;component/Recursos.xaml"

So I don't know why it doesn't recognize it. The reference is working as I can use every class from it.

所以我不知道为什么它不识别它。该参考正在工作,因为我可以使用它的每个类。

回答by DeshDeep Singh

Try like this it will work: Project ABC has a reference to Project XYZ.

像这样尝试它会起作用:Project ABC 引用了Project XYZ。

   <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Project XYZ;component/YourSubFolder/YourResourceFile.xaml" />
    </ResourceDictionary.MergedDictionaries>

Then you can just use the Resources defined in YourResourceFile.xaml.

然后您可以只使用在 YourResourceFile.xaml 中定义的资源。

回答by DeshDeep Singh

You need to add your ResourceDictionaryas:

你需要添加你ResourceDictionary的:

<ResourceDictionary Source="/MyCompany;component/Recursos.xaml" />

回答by dba

I assume this won't help you two years later. May do for someone else :) Make sure, to close VS and reopen the sln after referencing the external xaml... Did the trick for me... The XAML-Editor part is still not 100% (much better in VS2017 as it has been, but still... there are some issues like that)

我想两年后这对你没有帮助。可能会为其他人做:) 确保在引用外部 xaml 后关闭 VS 并重新打开 sln...对我有用... XAML-Editor 部分仍然不是 100%(在 VS2017 中好多了,因为它已经,但仍然......有一些类似的问题)

AND don't forget to add:

并且不要忘记添加:

System.Reflection.Assembly.LoadFrom("yourResourceAssemblyfullpath")

in the ctor of the Window/Control consuming the Resource BEFORE calling "InitializeComponent()"

在调用“InitializeComponent()”之前消耗资源的窗口/控件的构造函数中

    Sub New()

    System.Reflection.Assembly.LoadFrom("yourResourceAssemblyfullpath")
    InitializeComponent()

    End Sub

Otherwise you get a runtime error...

否则你会得到一个运行时错误...