0

I am developing an application, where I need to give user an option to change theme of app at any point of time through settings from dark to light and vice versa and can switch the theme at any time.

Please let me know if there is a way to do it.

Currently the app changes theme according to OS theme. If user has dark theme selected in OS, app will have dark theme and vice versa.

Requirement is irrespective of OS theme user should be able to change theme inside application (Like in Google Mail/ VLC player settings etc.)

Please help me out in implementing this.

Charan Ghate
  • 1,384
  • 15
  • 32

2 Answers2

0

If you made the application switch with the OS Theme by using the global resource keys for colors, icons and brushes you would have to replace all of that; as far as I know there is no way to intercept that.

To have the application switch between themes you could define a property on the main viewmodel that can be accessed and bound to from anywhere. Put all the colors and resources you want to theme in a ViewModel and bind to these properties. Next create some value converters to turn a general resource reference into a themed resource reference. That will allow you to specify a color by a logical name and have the converter translate it to a theme aware color:

<Text Background="{Binding Path=UIResources.TextBackground, 
                           Converter={StaticResource ColorToThemedColor}}" />
Emond
  • 50,210
  • 11
  • 84
  • 115
  • I have used global resources available for Windows Phone, which switch automatically once the user change theme of OS. But to make it app specific cant i use same global resources with some customized method at run time. – user3025964 Mar 10 '15 at 06:37
  • @user3025964 - yes, that is correct. Did you check this question: http://stackoverflow.com/questions/19272265/how-to-force-windows-phone-8-app-to-be-run-in-light-theme ? – Emond Mar 10 '15 at 07:04
  • But this changes the theme one time only at the starting of application. My concern is to have a toggle button or check box which on checked switches to Light theme and when unchecked switch to dark Theme. – user3025964 Mar 10 '15 at 10:38
  • In Windows Phone 8.1 you can use the RequestedTheme property https://msdn.microsoft.com/en-us/library/ie/windows.ui.xaml.frameworkelement.requestedtheme but in Windows Phone 8 there is no such thing and you would have to use a third party solution https://github.com/jeffwilcox/wp-thememanager Or write one yourself as I mentioned. – Emond Mar 10 '15 at 11:08
0

You could bind the RequestedTheme of your pages to a bool-Value.

Then write a value-converter, which returns Light or Dark for true/false, an set it as Page-Resource.

Last you have write a static Property, to save the value through navigation.

Initialize the static value on start up, and every constructor of you viewmodel get's the value at sets the isLight-Property on the Page

<Page RequestedTheme={Binding isLight,Converter={Staticresource TruetoLightConverter}>
Jmie
  • 97
  • 1
  • 12
  • RequestedTheme is present from Windows phone 8.1. I need to support Windows Phone 8 and above. So, requestedTheme is not there in Windows Phone 8. Let me know if there is any other way to get this done.... – user3025964 Mar 10 '15 at 06:34