Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change theme at runtime

Tags:

I have a WPF application with a theme (ShinyRed.xaml) and I want to have a button that when clicked changes the theme to ShinyBlue.xaml

I load in the theme initially in App.xaml:

<Application.Resources>     <ResourceDictionary Source="/Themes/ShinyBlue.xaml"/> </Application.Resources> 

How might I do this?

like image 604
Entity Avatar asked Jun 03 '11 16:06

Entity


People also ask

How to change theme at runtime in Android?

In your mobile, in the display settings, there is an option to set your whole mobile themes like a light blue color theme, red color theme or any other. This will set your full mobile functionality with this theme setting.

How do I change the theme code in Android Studio?

To change default themes go to File and click on Settings. A new Settings dialog will appear, like this. Under the Appearance & Behaviour -> Appearance, you will find Theme. Choose the right theme from the drop-down and click on Apply and then Ok.

How do I change my app theme?

In the Project pane select Android, go to app > res > values > themes > themes.

What is dynamic theme in Android?

This week, the Android 12 starts to be released by Google. With the update comes the new Dynamic Theme feature, which lets you use the colors of the wallpaper to customize the look of the system and other apps with support for Material You.


1 Answers

How you could do it:

<Application.Resources>     <ResourceDictionary>         <ResourceDictionary.MergedDictionaries>             <ResourceDictionary x:Name="ThemeDictionary">                 <ResourceDictionary.MergedDictionaries>                     <ResourceDictionary Source="/Themes/ShinyRed.xaml"/>                 </ResourceDictionary.MergedDictionaries>             </ResourceDictionary>         </ResourceDictionary.MergedDictionaries>     <!-- ... --> 
public partial class App : Application {     public ResourceDictionary ThemeDictionary     {         // You could probably get it via its name with some query logic as well.         get { return Resources.MergedDictionaries[0]; }     }      public void ChangeTheme(Uri uri)     {         ThemeDictionary.MergedDictionaries.Clear();         ThemeDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = uri });     }      //... } 

In your change method:

var app = (App)Application.Current; app.ChangeTheme(new Uri("New Uri here")); 
like image 82
H.B. Avatar answered Oct 14 '22 19:10

H.B.