WP7 Mango ResouceDictionary MergedDictionaries

I have a problem with ResourceDictionary in WP7 Mango.

Most of what I can find on the internet is simple:

1) Xaml file with body:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 <Style x:Key="TextBlockStyle1" TargetType="TextBlock">
 <Setter Property="Foreground" Value="Orange"/>
 <Setter  Property="FontSize" Value="24"/>
 <Setter  Property="VerticalAlignment" Value="Bottom"/>
</Style>
</ResourceDictionary>

      

2) Add the following to App.xaml:

 <Application.Resources>
    <ResourceDictionary>
       <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="MyResources.xaml"/>
       </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
 </Application.Resources>

      

Not sure why it doesn't work. When I do this, I get an exception:

The 'ResourceDictionary' type is inside a ResourceDictionary and has no key.

When I add ked to the second line of the xaml in step 2, it starts but fails with an undefined error. It looks like it doesn't add resouces from the MyResources.xaml file.

Can anyone point out a solution here?

+3


source to share


2 answers


You need to set a key for the ResourceDictionary in App.xaml.



<Application.Resources>
    <ResourceDictionary x:Key="keyname">
       <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="MyResources.xaml"/>
       </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

      

+1


source


Actually made it up.

I was trying to get it to work without a key and found out that the styles I left in App.xaml were causing the problem. So any remaining styles left in App.xaml i had to move internally even though they were unique.

<Application.Resources>
<ResourceDictionary>

   my remaining styles with key & target type are here now

   <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="MyResources.xaml"/>
   </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

      

EDIT:

Few more important details that could have saved me time and took me time to figure it out: 1) As MSDN recommends, you shouldn't put the key in the ResourceDictionary



2) Styles inside referenced Xaml must contain a key (or name)

3) The rest of the styles must be placed as described above

4) In the following code, if you override the base style on which some of the other styles are based, the changes will not be reflected until you override the inherited styles in MyResources2.xaml too (alternatively replace the base style in MyResources.xaml with the style in MyResources2 .xaml)

<ResourceDictionary.MergedDictionaries>
  <ResourceDictionary Source="MyResources.xaml"/>
  <ResourceDictionary Source="MyResources2.xaml"/>             
</ResourceDictionary.MergedDictionaries> 

      

5) ResourceDictionaries in MergedDictionaries function like LIFO

+1


source







All Articles