The resource "x" could not be resolved.

33,474

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

it is possible to use the relative path to your appStyles.xaml folder. hope this helps.

something like below

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

Solution 5

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

Share:
33,474
Chrisjan Lodewyks
Author by

Chrisjan Lodewyks

Updated on December 06, 2020

Comments

  • Chrisjan Lodewyks
    Chrisjan Lodewyks over 3 years

    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?

  • Chrisjan Lodewyks
    Chrisjan Lodewyks about 11 years
    I have cleaned, rebuilt, restarted and even restarted my computer! This error keeps popping up and it makes no sense, it was running perfectly one second and the next it could no longer compile!
  • hijack
    hijack about 11 years
    Hmm. This is so strange. Have you tried to rename your Style keys?
  • Chrisjan Lodewyks
    Chrisjan Lodewyks about 11 years
    Yes I have, the problem remains! It does not stop me from compiling anymore, but none of my custom controls that use this style can be showed in design!
  • superjos
    superjos about 9 years
    switching the dictionary build action to Resource does not change anything