Dictionary of nested resources in a separate library

My question is very similar to this one

I have a solution with a number of projects. These are two relevant ones: a class library containing a WPF window and a project with all WPF styles.

Class Library with Window in Project 1

A dictionary with a merged window looks something like this:

<ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/StyleAssembly;component/CommonStyle.xaml"/> </ResourceDictionary.MergedDictionaries> //other styles here 

CommonStyle.xaml in Project 2 :

 <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Components/Type/CheckBox.xaml"/> </ResourceDictionary.MergedDictionaries> 

This leads to errors, for example:

{"Unable to find resources" /type/checkbox.xaml '. "}

However, if I create CommonStyle.xaml in Project 1 and reference the same management styles from Project 2, then it works.

How to create the highest level xaml file (CommonStyle.xaml) from project 2?

+3
source share
1 answer

I cannot verify this at this time, but I hope it works.

Instead of the root path, use the relative path in Project 2, i.e.

 <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Components/Type/CheckBox.xaml"/> </ResourceDictionary.MergedDictionaries> 

You can also use .. to navigate to the relative directory (depending on the location of CommonStyle.xaml ), for example

 <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="../Components/Type/CheckBox.xaml"/> </ResourceDictionary.MergedDictionaries> 

I believe that when you use the root path (starting with / ), it will look for CheckBox.xaml in the root of the project where you use CommonStyle.xaml , and not relative to the location of CommonStyle.xaml .

Additional explanation

From your description, it looks like you have the following structure:

 - Project 1 - Window.xaml - Project 2 - CommonStyle.xaml - Components - Type - CheckBox.xaml 

When CommonStyle.xaml refers to / , it usually refers to the root of project 2, however when you merge it into Window.xaml , / will now refer to the root of project 1, then it will not be able to find Components/Type/CheckBox.xaml .

By removing / , he will now look for Components/Type/CheckBox.xaml relative to the location of CommonStyle.xaml that he can do.

+3
source

Source: https://habr.com/ru/post/1259723/


All Articles