views:

155

answers:

1

I have defined a custom resourceDictionary and I have added it to the app.xaml resources

I have a style with key "MyStyle" in the Theme.xaml. How can I access the MyStyle style from the MainPage.xaml?

A: 

Have you tried this in your App.xaml:-

 <Application.Resources>
   <ResourceDictionary>
     <!-- You other resources here -->
     <ResourceDictionary.MergedDictionaries>
       <ResourceDictionary Source="Theme.xaml" />
     </ResourceDictionary.MergedDictionaries>
   </ResourceDictionary>
 </Application.Resources>
 ...

Now you should be able to {StaticResource MyStyle} from within MainPage.xaml.

AnthonyWJones
I have tried it but it throws me "Elements in a ResourceDictionary must have x:Key or x:Name attribute. [Line: 43 Position: 29]". However when I do the following in app.xaml: <ResourceDictionary x:Key="ResourceDictionary"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Resources/Theme.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>No exception is thrown. But now I don't know how to access MyStyle
For this to work the Application.Resources should contain a single outer ResourceDictionary (no need to name or key it), its inside this element that you would add the resources you would normally place directly in Application.Resources. At the bottom of this ResourceDictionary you place the ResourceDictionay.MergedDictionaries value.
AnthonyWJones
I have tweaked y answer to remove the / prefix, that broke it in testing, as its it works fine.
AnthonyWJones