12

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
  • 1,162
  • 4
  • 13
  • 25
  • Do you mean you get problems during design time about something not being found? But you can compile and it runs fine, right? – Meirion Hughes Mar 05 '13 at 13:26
  • I get errors in design time saying that the resource is not found. But sometimes I can run, and it even displays the buttons correctly! But then other times it stops it from debugging. – Chrisjan Lodewyks Mar 05 '13 at 13:35
  • Do you have code behind for AppStyles.xaml? (I did and that stopped VS from loading it in design time) Another problem may be including AppStyles.xaml as a Resource which works at compile time, but not in design-time. It should be a 'Page'. – Wouter Apr 28 '14 at 21:08

7 Answers7

16

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

WonkiDonk
  • 319
  • 2
  • 7
6

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

Community
  • 1
  • 1
hijack
  • 263
  • 2
  • 3
  • 11
  • 1
    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! – Chrisjan Lodewyks Mar 06 '13 at 07:24
  • Hmm. This is so strange. Have you tried to rename your Style keys? – hijack Mar 06 '13 at 08:32
  • 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! – Chrisjan Lodewyks Mar 06 '13 at 08:45
4

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.

xariez
  • 529
  • 1
  • 5
  • 17
3

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>
AMS
  • 550
  • 6
  • 22
1

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

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

or simply let the vs2012 process it by using the properties tab...

1

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

CoRe23
  • 131
  • 2
  • 5
0

Also you can check for styles with the same x:key in AppStyles.xaml. It caused me a similar error.

Mikhail
  • 357
  • 1
  • 3
  • 17