如何从外部程序集引用XAML DictionaryResource?

时间:2021-02-11 19:54:38

I'm a total Nubie to the world of WPF. I'm just starting to get immersed in it.

我是WPF世界的Nubie。我刚刚开始沉浸其中。

I've got an issue I'm trying to figure out with using ResourceDictionary and Merged Dictionaries.

我有一个问题,我试图弄清楚使用ResourceDictionary和Merged Dictionaries。

I've got WPF sample application that I've converted to a Class Library. That newly converted assembly is included/referenced in a library of user controls that I'm trying to create. That converted assembly is included a project in the solution containing my user control library (dll) project.

我有WPF示例应用程序,我已转换为类库。在我正在尝试创建的用户控件库中包含/引用新转换的程序集。转换后的程序集包含在包含我的用户控件库(dll)项目的解决方案中的项目中。

I was able to convert it successfully and got rid of all the errors except for some issues with the ResourceDictionary.

我能够成功转换它并除去所有错误,除了ResourceDictionary的一些问题。

This is what the offending XAML looks like:

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/ConvertedAppPath/Dictionaries/Brushes.xaml" />
                <ResourceDictionary Source="/ConvertedAppPath/Dictionaries/Styles.xaml" />
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

The original WPF application had several Resource Dictionary items included in the assembly.

原始WPF应用程序在程序集中包含多个Resource Dictionary项。

When I converted to a class library, I'm getting the following error message on resource dictionary:

Error 21 An error occurred while finding the resource dictionary "/ConvertedAppPath/Dictionaries/Brushes.xaml". C:\MyExplicitPath\WPFUserControls\DynamicDataChart.xaml 15 17 WPFUserControls Error 22 An error occurred while finding the resource dictionary "/ConvertedAppPath/Dictionaries/Styles.xaml". C:\MyExplicitPath\WPFUserControls\DynamicDataChart.xaml 15 17 WPFUserControls

错误21查找资源字典“/ConvertedAppPath/Dictionaries/Brushes.xaml”时发生错误。 C:\ MyExplicitPath \ WPFUserControls \ DynamicDataChart.xaml 15 17 WPFUserControls错误22查找资源字典“/ConvertedAppPath/Dictionaries/Styles.xaml”时发生错误。 C:\ MyExplicitPath \ WPFUserControls \ DynamicDataChart.xaml 15 17 WPFUserControls

So, I thought that perhaps I needed a relative path for the ResourceDictionaries.

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../ConvertedAppPath/Dictionaries/Brushes.xaml" />
                <ResourceDictionary Source="../ConvertedAppPath/Dictionaries/Styles.xaml" />
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

I get the following error message(s) inside the XAML editor:

The file "../ConvertedAppPath/Dictionaries/Brushes.xaml" is not part of the project or it's Build Action property is not set to "Resource".

文件“../ConvertedAppPath/Dictionaries/Brushes.xaml”不是项目的一部分,或者它的Build Action属性未设置为“Resource”。

I'd really like to understand this, so I've got a couple of questions:

1) How do I clean this up to get rid of the errors?

1)如何清理它以消除错误?

2) Do I need to explicitly set the assembly location in "Source"?

2)我是否需要在“Source”中明确设置装配位置?

3) What are all the pieces of the "Source" property and what do they really mean?

3)“来源”财产的所有部分是什么?它们的真正含义是什么?

4) Why did the 2nd error message not get displayed at compile time, but is visible only in the XAML editor?

4)为什么第二条错误消息在编译时没有显示,但只在XAML编辑器中可见?

Thanks for your response and efforts, JohnWPFNewB

感谢您的回应和努力,JohnWPFNewB

1 个解决方案

#1


0  

be sure that '/ConvertedAppPath/Dictionaries/Brushes.xaml' properties set 'copy always'

确保'/ConvertedAppPath/Dictionaries/Brushes.xaml'属性设置'始终复制'

Because when you run the project Brushes.xaml should copy to same directory with yourproject.exe as same with the path which is given in the xaml (/ConvertedAppPath/Dictionaries/Brushes.xaml)

因为当您运行项目时,Brushes.xaml应该使用yourproject.exe复制到同一目录,与xaml中给出的路径相同(/ConvertedAppPath/Dictionaries/Brushes.xaml)

#1


0  

be sure that '/ConvertedAppPath/Dictionaries/Brushes.xaml' properties set 'copy always'

确保'/ConvertedAppPath/Dictionaries/Brushes.xaml'属性设置'始终复制'

Because when you run the project Brushes.xaml should copy to same directory with yourproject.exe as same with the path which is given in the xaml (/ConvertedAppPath/Dictionaries/Brushes.xaml)

因为当您运行项目时,Brushes.xaml应该使用yourproject.exe复制到同一目录,与xaml中给出的路径相同(/ConvertedAppPath/Dictionaries/Brushes.xaml)