WPF ResourceDictionary cannot locate source - wpf

My XAML designer keep screaming "Cannot locate resource xxx.xaml". I have tried both pack application and absolute path URI formats
"pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml"
"pack://application:,,,/Subfolder/ContentFile.xaml"
"/AssemblyName;component/FilePath.xaml"
The project file structure (can't post picture, not enough rep):
root/App.xaml
root/MainWindow.xaml
root/Res
root/Res/PalleteBlue.xaml
root/UserControl
root/UserControl/NavigationDrawer.xaml
root/UserControl/Res
root/UserControl/Res/NavigationDrawerResource.xaml
NavigationDrawer.xaml use NavigationDrawerResource.xaml
MainWindow.xaml have instance of NavigationDrawer.xaml
XAML designer on NavigationDrawer has no issues, but on MainWindow it keep telling Cannot locate resource usercontrol/res/navigationdrawerresource.xaml
Here is resource declaration in NavigationDrawer.xaml
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/UserControl/Res/NavigationDrawerResource.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Res/PalleteBlue.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
And here is how MainWindow.xaml add instance of NavigationDrawer
xmlns:uc="clr-namespace:InfiniteLite.View.UserControl"
<Grid>
<uc:NavigationDrawer></uc:NavigationDrawer>
</Grid>
This yields the Cannot locate resource.... error on <uc:NavigationDrawer> tag but the project can debug just fine

Related

Multiple XAML resource files in one assembly, referenced in another assembly

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.
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!
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).

ResourceDictionary makes use of UserControl -> Resources within UserControl cannot be resolved

I have Resources defined as a ResourceDictionary in App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/AssemblyName;component/Resources/HamburgerMenuStyles.xaml" />
<!--…-->
</ResourceDictionary.MergedDictionaries>
<converters:LanguageConverter x:Key="LanguageConverter"/>
<!--…-->
</ResourceDictionary>
</Application.Resources>
Then, in the ResourceDictionary "HamburgerMenuStyles.xaml" that is included in above code I make use of a UserControl:
<mah:HamburgerMenu.OptionsItemsSource>
<mah:HamburgerMenuItemCollection>
<mah:HamburgerMenuIconItem Label="{localization:Loc tabConf}">
<mah:HamburgerMenuIconItem.Icon>
<!--…-->
</mah:HamburgerMenuIconItem.Icon>
<mah:HamburgerMenuIconItem.Tag>
<controls:Settings/> <!--Calling the UserControl here-->
</mah:HamburgerMenuIconItem.Tag>
</mah:HamburgerMenuIconItem>
</mah:HamburgerMenuItemCollection>
</mah:HamburgerMenu.OptionsItemsSource>
I get errors, that resources used within the UserControl (the resources used there are also placed in App.xaml) cannot be resolved, eventhough the code compiles and works. How come? I do not get intellisense errors in the UserControl.xaml itsself.
Example:
Visual Studio complains about a converter used in the UserControl which is defined as a resource in App.xaml and does not show any errors in the control xaml + compiles and works.
Adding the "missing" resource to the control, compiling and then removing the resource again resolved my problem:
<mah:HamburgerMenu.OptionsItemsSource>
<mah:HamburgerMenuItemCollection>
<mah:HamburgerMenuIconItem Label="{localization:Loc tabConf}">
<mah:HamburgerMenuIconItem.Icon>
<!--…-->
</mah:HamburgerMenuIconItem.Icon>
<mah:HamburgerMenuIconItem.Tag>
<controls:Settings>
<controls:Settings.Resources>
<converters1:InvertBoolConverter x:Key="InvertedBoolConverter"/>
</controls:Settings.Resources>
</controls:Settings>
</mah:HamburgerMenuIconItem.Tag>
</mah:HamburgerMenuIconItem>
</mah:HamburgerMenuItemCollection>
</mah:HamburgerMenu.OptionsItemsSource>
This seems to have been a bug somehow. No problems remaining :-)

How to reference an app resource in a xaml Window?

I've added a reference to an app resource in my View's window. But when I declare the resource I get the error:
Error 10 RegexValidationRule is not supported in a Windows Presentation Foundation (WPF) project.
This is how I added the resource to the View:
<Window.Resources>
<validators:RegexValidationRule x:Key="localRegexValidationRule"/>
</Window.Resources>
Does anyone know how to reference the resource correctly in WPF?
I've followed this solution but still get the same error on the resource. This is how I've declared the resource in app.xaml within a resource dictionary:
<Application x:Class="MongoDBApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:MongoDBApp.Converters"
xmlns:validators="clr-namespace:MongoDBApp.Validators"
StartupUri="pack://application:,,,/Views/MainView.xaml">
<Application.Resources>
<ResourceDictionary>
<converters:BoolToNonVisibilityConverter x:Key="localBoolToNonVisibilityConverter" />
<converters:BoolToVisibilityConverter x:Key="localBoolToVisibilityConverter" />
<validators:RegexValidationRule x:Key="localRegexValidationRule" />
</ResourceDictionary>
</Application.Resources>
</Application>
Have you added the appropriate using statement in your View file? It should have the same statement as your App.xaml file:
xmlns:validators="clr-namespace:MongoDBApp.Validators"

ResourceDictionary Merge behaviour

In my project I am trying to embed source code from Avalon Wizard but there is something strange happening and I am not able to successful integrate it.
I have the following structure in my Custom control library
Wizard
Themes
Aero.NormalColor.xaml
Aero2.NormalColor.xaml
AeroWizardHeader.xaml
Generic.xaml
Wizard97.xaml
WizardAero.xaml
Generic.xaml
Themes
Generic.xaml
Inside my Generic.xaml I have the following delcaration
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CuratioCMS.Client.UI;Component/Wizard/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
and inside Wizard/Themes/Generic.xaml here is how I have my MergedDictionaries defined
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CuratioCMS.Client.UI;Component/Wizard/Themes/Wizard97.xaml" />
<ResourceDictionary Source="CuratioCMS.Client.UI;Component/Wizard/Themes/WizardAero.xaml" />
<ResourceDictionary Source="CuratioCMS.Client.UI;Component/Wizard/Themes/AeroWizardHeader.xaml" />
</ResourceDictionary.MergedDictionaries>
According to Avalon implementation there is one more MergedDictionary provided inside Aero.NormalColor.xaml which looks like this
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CuratioCMS.Client.UI;Component/Wizard/Themes/AeroWizardHeader.xaml" />
</ResourceDictionary.MergedDictionaries>
My problem is that I am not able to use my own class library in my project as designer throws exception which is: Cannot locate resource 'wizard/themes/curatiocms.client.ui;component/wizard/themes/wizard97.xaml'.
If I change Path inside Wizard/Theme/Generic.xaml then error goes away but Wizard is not able to provide Aero functionality and even designer does not work ex expected.
Let me also say, that I decided to have this kind of architecture because according to extended WPF toolkit this is very clean way .
can anyone help me with this?
Have you tried using the full pack URI syntax?
Have you tried changing 'Component' to 'component'? Also make sure the dictionary has it's build action set to Resource and you are correctly referencing your CuratioCMS.Client.UI assembly.

Global style in a separate assembly

For my application themes, I created a separate class library MyApp.Themes.dll.
In the root folder of this library I have Standard.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:,,,/MyApp.Themes;component/Standard/Accordion.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
In Standard folder I have Accordion.xaml:
<ResourceDictionary xmlns:layoutPrimitivesToolkit="..."
xmlns:layoutToolkit="..."
...>
<!-- layoutPrimitivesToolkit:AccordionButton -->
<Style TargetType="layoutPrimitivesToolkit:AccordionButton">
...
</Style>
...
</ResourceDictionary>
which are default styles for WPF Toolkit Accordion control, except that style for
AccordionItem is modified to set background to transparent (instead of blue, which is a known bug).
For both xaml files Build Action is set to "Resource".
Now, after referencing MyApp.Themes library in MyApp WPF project, in App.xaml I wrote the following:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/MyApp.Themes;component/Standard.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
It doesn't work (no error; just style not applied). If I copy&paste Accordion.xaml styles directly in App.xaml, then it works.
Some people suggested adding dummy resource dictionary, so I added the following in App.xaml after MergedDictionaries ending tag:
<Style TargetType="{x:Type layoutToolkit:AccordionItem}" />
Again, it doesn't work. Can someone help me solve this? By the way, I didn't try an approach
suggested by some others - using basedOn. If I must do that, I'd like to do in MyApp.Themes,
because it makes using themes nicer in App.xaml. Thank you in advance.
UPDATE1: It works if I directly copy&paste Accordion.xaml content to Standard.xaml
UPDATE2: I tried local assembly resource file URI (http://msdn.microsoft.com/en-us/library/aa970069.aspx) in Standard.xaml:
<ResourceDictionary Source="pack://application:,,,/Standard/Accordion.xaml"/>
It throws an error, saying standard/accordion.xaml not found.
It seems there's a bug in .NET. See the thread below:
Trouble referencing a Resource Dictionary that contains a Merged Dictionary
Also, there's a connect page on Microsoft site:
https://connect.microsoft.com/VisualStudio/feedback/details/609601/merge-dictionaries-does-not-work-when-we-merge-merged-dictionaries#tabs

Resources