Reputation: 481
I'm in the process of building a WPF application and ran into an error when trying to reference a resource dictionary. Inside my WPF application project I have a folder called "Styles" to contain all the xaml style templates for my app:
In my app.xaml file I have this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/MetroTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
However, when I hover over the source property I get an error saying "An error occurred while finding the resource dictionary "Styles/MetroTheme.xaml". I can see the xaml file inside the folder both in Visual Studio and in the file system.
I have also tried "/Styles/MetroTheme.xaml" and a pack uri for the source property, both with no success. Any idea why I'm getting this file not found error?
Upvotes: 12
Views: 17750
Reputation: 1177
I confirm it because of some strange behavior on relative uri.
For example:
/SubProjectDir/WhatThingDir/YourResource.xaml
<ResourceDictionary> <SolidColorBrush x:Key="TheBrush" Color="Black" /> </ResourceDictionary>
/SubProjectDir/AnotherDir/YourControl.xaml
... <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/WhatThingDir/YourResource.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> ...
/MainExecution/MainWindow.xaml
... <sub:YourControl /> ...
It looks work well in designer. But when you run it, will throw cannot find resource execption.
Because /WhatThingDir/YourResource.xaml
means "Find the related resource from current root path", but now the MainWindow
constructed YourControl
, so the current root path is start under MainExecution
. Then, it try to find the resource path /WhatThingDir/YourResource.xaml
in MainExecution
. That's must fail!
But if you write this, it will works be fine:
/SubProjectDir/AnotherDir/YourControl.xaml
... <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="../WhatThingDir/YourResource.xaml" /> <!-- Here ^^^ Here --> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> ...
That's will seek the resource in /SubProjectDir/AnotherDir/../WhatThingDir/YourResource.xaml
=> /SubProjectDir/WhatThingDir/YourResource.xaml
.
It's ok!
Upvotes: 0
Reputation: 119
Sometimes closing Visual Studio and open it again solves this problem.
Upvotes: 6
Reputation: 1
Change the target .net version to an older one in the project properties and then reset to the previous version.
Upvotes: -1
Reputation: 1436
I had the same issue, but setting Build Action = Page did not solve for me. Turned out I needed to use the Pack URI Format. So,
<ResourceDictionary Source="pack://application:,,,/Styles/MetroTheme.xaml"/>
EDIT
Turns out the above will eliminate build error but still results in runtime error. I needed to include full assembly specification for complete resolution (even though all files are in same assembly):
<ResourceDictionary Source="pack://application:,,,/WpfApplication10;component/Styles/MetroTheme.xaml"/>
Upvotes: 11