Resource "x" cannot be resolved.

I recently upgraded to VS 2012, I needed to start using .net 4.5, but this is not relevant. My problem is this:

I have a ResourceDictionary in my main project called AppStyles.xaml , and in App.Xaml I have the following:

 <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="AppStyles.xaml"/> <ResourceDictionary.MergedDictionaries> </ResourceDictionary> 

There is a style in my ResourceDictionary , and I could apply this style to any button in any of my projects by setting Style={StaticResource MyButton} .

After upgrading to VS 2012, I noticed that I get an error in the name a lot, and sometimes it prevents me from debugging, and sometimes not!

Is there any other way I have to do this or is there a problem in VS 2012?

+6
source share
5 answers

WPF cannot resolve the resource dictionary you are trying to merge. When you create a federated dictionary, WPF follows a set of rules to try to resolve the URI. In your case, the URI is ambiguous, so WPF cannot reliably resolve it.

Change the Source URI in App.xaml to the absolute package URI. For example, if the project is called MyProject (ie, “MyProject” is the short name of the assembly), you should change Source in App.xaml to:

  <ResourceDictionary Source="/MyProject;component/AppStyles.xaml"/> 

This assumes that AppStyles.xaml is at the root of MyProject . You can optionally specify authority, assembly version, and public key information of the signed assembly. However, you must be safe with the short assembly name (MyProject, in the example above).

See this page on MSDN for more information on the package URI in WPF: http://msdn.microsoft.com/en-us/library/aa970069 (v = vs .110) .aspx

+10
source

I also have this problem from time to time in VS2010. Sometimes the problem will be solved if I make a “Clean Solution” and “Reconstruction of the Solution”. If this does not work, I usually restart VS2010.

I also renamed Style x: Key to something else, and the problem disappeared. But I do not think this solution is ideal ...

 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <!-- Load Infrastructure Resource Dictionaries --> <ResourceDictionary Source="/MyProject.Modules.Infrastructure;component/ResourceDictionaries/ResourceLibrary.xaml" /> </ResourceDictionary.MergedDictionaries> <!-- Workaround for ResourceDictionary loading bug/optimization --> <Style TargetType="{x:Type Rectangle}" /> </ResourceDictionary> 

Link to this question regarding the Workaround in my code example: Problem with a help resource containing a federated dictionary

+5
source

You can create a shared or infrastructure project that other projects will link to. Add resources for reuse. Then create the package URI. then a link in your resources usercontrol or window in the resource dictionary

 <...Resources> <ResourceDictionary> <!-- Resource Dictionaries --> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/Common;component/Dictionaries/Styles.xaml"/> </ResourceDictionary.MergedDictionaries> 

In usercontrol or use Window.Resources in case of a window. This works for me.

+4
source

You can use the relative path to the appStyles.xaml folder. hope this helps.

something like below

 <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="../Resources/Styles.xaml"></ResourceDictionary> </ResourceDictionary.MergedDictionaries> <ResourceDictionary> 
+3
source

you can add the resource dictionary to your project, and then change its build action to the resource, and then try to add its uri to the code as follows:

  <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Styles.xaml"/> </ResourceDictionary.MergedDictionaries> 

or just let vs2012 handle it using the properties tab ...

+1
source

All Articles