Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The resource "x" could not be resolved.

I have recently upgraded to VS 2012, I had to as I needed to start using the .net 4.5 but that is besides the point. My problem is the following:

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

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

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

After upgrading to VS 2012 I noticed I get the error in the title a lot, and sometimes it stops me from debugging and sometimes it doesn't!

Is there a different way that I should be doing this or is there a problem in VS 2012?

like image 909
Chrisjan Lodewyks Avatar asked Mar 05 '13 13:03

Chrisjan Lodewyks


4 Answers

I had placed some Style in Window.Resources a this caused my problem

After deleting all styles from MainWindow problem disappeared.

Found second problem:

Problem was Platform Target set to x64.

After changing it to AnyCPU the resources in design works..

like image 152
CoRe23 Avatar answered Oct 11 '22 14:10

CoRe23


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

Change the Source URI in the App.xaml to an absolute pack URI. For example, if you project is called MyProject (i.e.: "MyProject" is the short assembly name), then you should change the Source in App.xaml to:

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

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

See this page on MSDN for further details on Pack URIs in WPF: http://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx

like image 34
WonkiDonk Avatar answered Oct 11 '22 13:10

WonkiDonk


I also have this problem time to time in VS2010. Sometimes the problem will solve if I make a "Clean Solution" and a "Rebuild Solution". If that do not work I usually restart VS2010.

I have also renamed the Style x:Key to something else and the problem was gone. But I dont find this solution to be perfect...

<Application.Resources>
<ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>

    <!-- Load Infrastructure's 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>

Reference to this question regarding the Workaround in my code sample: Trouble referencing a Resource Dictionary that contains a Merged Dictionary

like image 30
hijack Avatar answered Oct 11 '22 13:10

hijack


you can create a common or infrastructure project from which other projects will reference. Add your resouce resources. Then create a pack URI. then reference in your usercontrol or window resources within a resource dictionary

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

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

like image 23
xariez Avatar answered Oct 11 '22 14:10

xariez