Spook
Spook

Reputation: 25927

Windows Phone, how to react to theme change?

I'm using themed resources:

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Dark" Source="../Styles/Main/Dark.xaml" />
            <ResourceDictionary x:Key="Light" Source="../Styles/Main/Light.xaml" />
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Page.Resources>

This solution generally works - but only if theme was chosen before starting up the application. If user changes theme during application runtime, all themed colors (ones accessible via StaticResource) are refreshed correctly, but application still uses old theme dictionary (for instance, Dark, when user switched from Dark to Light).

How can I interrupt theme change and load valid theme dictionary?

Upvotes: 0

Views: 288

Answers (1)

Bret Bentzinger
Bret Bentzinger

Reputation: 376

The resources defined with ThemeResource should update automagically when the user changes the phone theme. Check out the remarks section here:

http://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn263118.aspx

Specifically:

"When the app first starts, any resource reference made by a ThemeResource reference is evaluated based on the theme in use at startup. But if the user subsequently changes the active theme at run-time, the system will re-evaluate every ThemeResource reference, retrieve a theme-specific resource that may be different, and redisplay the app with new resource values in all appropriate places in the visual tree."

Make sure to mark the resources in your Theme dictionaries as a ThemeResource and not a StaticResource.

Upvotes: 1

Related Questions