4
votes

I have a Windows 10 universal app with a few resource dictionaries for styles and such. In one such resource dictionary, I have a color, MyBlue that I want to access via the code behind. How is this achieved?

I've tried this.Resources["MyBlue"], but since MyBlue is defined in a separate resource dictionary and not directly in the page resources it doesn't exist in this collection.

Here's my app.xaml:

<Application.Resources>

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/PodStyles.xaml"/>
            <ResourceDictionary Source="Styles/ButtonStyles.xaml"/>
            <ResourceDictionary Source="Styles/OfferStyles.xaml"/>
            <ResourceDictionary Source="Styles/MyStyles.xaml"/>

            <ResourceDictionary>
                <!--Global View Model Locator-->
                <vm:ViewModelLocator x:Key="Locator"
                                     d:IsDataSource="True"/>
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</Application.Resources>
4

4 Answers

5
votes

Have you tried

Application.Current.Resources["MyBlue"]
4
votes

MergedDictionaries are available via Application.Current.Resources.MergedDictionaries.

I was able to obtain the appropriate ResourceDictionary by Uri and then access the item in it by key name:

var mergedDict = Application.Current.Resources.MergedDictionaries.Where(md => md.Source.AbsoluteUri == "ms-resource:///Files/Styles/MyStyles.xaml").FirstOrDefault();
newColor = (Windows.UI.Color)mergedDict["MyBlue"];
1
votes

The ResourceDictionary you want to reference has to be pulled into the application resources somehow. Either merge it into your Application's resources (if it's common enough to be included all the time, at a slight hit to your app's initialization), or merge it into your Page's resources (at a slight hit to the first page initialization).

You include foreign ResourceDictionaries with this syntax:

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="path/to/resource.xaml" />
        </ResourceDictionary>

        <... your other resources for this page go here .../>
    </ResourceDictionary>
</Page.Resources>
1
votes

I will try to sum-up all the solution as all previous solution seems to have part of the whole answer.

So there's two main cases :

Case A. Your value is in the main ResourceDictionary of your App.xaml

Where App.xaml is :

...
<Application.Resources>
    <ResourceDictionary>
        <Color x:Key="backgroundColor">LightBlue</Color>
    </ResourceDictionary>
</Application.Resources>
...

If so read your value by using :

Application.Current.Resources["backgroundColor"]

Case B. Your value is in an external ResourceDictionary merged in your App.xaml

Where App.xamlis :

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

Where Constants.xaml is :

<?xml version="1.0" encoding="utf-8" ?>
<?xaml-comp compile="true" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <Color x:Key="backgroundColor">LightBlue</Color>
</ResourceDictionary>

In this case use @earthling solution :

var mergedDict = Application.Current.Resources.MergedDictionaries.Where(md => md.Source.OriginalString.Equals("Styles/Constants.xaml")).FirstOrDefault();
var color = (Color)mergedDict["pressedColor"];