Good evening. A WPF XAML resource question please.
I have one user-control project called Resources. In this project I have a directory that contains multiple XAML files. I have merged these resources in a Main.xaml file in the project root directory.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources;component/xaml/Buttons.xaml" />
<ResourceDictionary Source="Resources;component/xaml/Images.xaml" />
<ResourceDictionary Source="Resources;component/xaml/Styles.xaml" />
<ResourceDictionary Source="Resources;component/xaml/Tooltips.xaml" />
</ResourceDictionary.MergedDictionaries>
The Resource project compiles without issue and there are no visual errors in the code you see above.
I have a second project called Buttons. In the App.xaml, I reference this Resource project.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources;component/Main.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I have referenced Resources.dll in the Button project. Visual Studio displays an error line under the ResourceDictionary statement; howbeit the project compiles.
When I run Button.exe, I get the error "Cannot locate resource 'resources;component/xaml/buttons.xaml'."
My ResourceDictionary statement matches what I have successfully done in other projects. Assuming what I have written in App.xaml in the Button project is correct, what should I do differently in the Resources' Main.xaml file?
Thanks in advance.
CodePudding user response:
Okay, I figured it out. I'll document how to do this in case anyone else runs across this.
In my one assembly entitled Resources, I have a Main.xaml file in the project root. I have a subdirectory entitled "xaml" where I have several xaml files. My mistake was in the leading backslash on the directory "xaml".
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="xaml/Buttons.xaml" />
<ResourceDictionary Source="xaml/Images.xaml" />
<ResourceDictionary Source="xaml/Styles.xaml" />
<ResourceDictionary Source="xaml/Tooltips.xaml" />
</ResourceDictionary.MergedDictionaries>
Then, in another project called Buttons, in the App.xaml file, I reference that Main.xaml file in the MergedDictionaries section.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources;component/Main.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Once I corrected the references in the main file, everything works correctly.
So this allows me to split up my resources into separate xaml files and reference them in other assemblies. Wohoo!
CodePudding user response:
Also, for completeness, for each of the xaml files (including main.xaml), set the Build Action to Page (right-mouse click on the xaml file and select Properties).