locked
Theme change at runtime

    Question

  • Hi,

    Any way to change theme of whole Application(switching style) without restating APP in window 8.1. I have seen many example but they did not work to change theme at run time with out restating app(Use custom style, don't use light ad dark theme)

    Monday, August 11, 2014 10:58 AM

Answers

All replies

  • If you define all styles for your theme wihin a ResourceDictionary, you could replace all resources of the application with this specific ResourceDictionary:

    ResourceDictionary rd = new ResourceDictionary();
    rd.Source = new Uri("Common/MyNewTheme.xaml");
    App.Current.Resources.Clear(); //clear existing theme...
    App.Current.Resources.MergedDictionaries.Add(rd); //set new theme...

    You cannot set the Application.RequestedTheme to a new value (Light or Dark) during runtime though.

    Monday, August 11, 2014 11:32 AM
  • I have two resource Dictionary Appstyle1 and Appstyle2.

    Set Appstyle1 in App.xaml like this.

    <Application.Resources>

      <ResourceDictionary>

       <ResourceDictionary.MergedDictionaries>

         <ResourceDictionarySource="ApplicationStyle/AppStyle1.xaml"/>

       </ResourceDictionary.MergedDictionaries>

      </ResourceDictionary>

    </Application.Resources>

    When application start Appstyle1 theme is showing in application.

    now I want to change this theme(Appstyle1 change into Appstyle2) at run time .

    I am trying blew code to change theme at runtime

    Write this function in App.Xaml.cs

    public void ChangeTheme(string themeUrl)
      {                                 
        ResourceDictionary resourceDict = newResourceDictionary();
        resourceDict.Source = new Uri(themeUrl,UriKind.Absolute);
        App.Current.Resources.Clear();
        App.Current.Resources.MergedDictionaries.Add(resourceDict);
      }

    Call above function from any application page like this:-

    private void Button_Click_1(object sender, RoutedEventArgs e)
        {
          var app = App.Current as App;
          string themeUrl = "ms-appx:///ApplicationStyle/AppStyle2.xaml";
           app.ChangeTheme(themeUrl);
    
        }
     But it is not show any change it showing previous theme. any help will be appreciated

       


                   


                   

               


           


       


    Tuesday, August 12, 2014 5:20 AM
  • You may want to use ThemeManagerRt, or take a look at its source code.
    Tuesday, August 12, 2014 6:06 AM
  • Yes, ThemeManagerRt defined such a good approach for change multiple theme at runtime. I created a sample through ThemeManagerRt, but its bad luck still I am not able to change theme at runtime.

    Can you see my sample what's wrong in it, Sample Code

    Thanks.


    Thanks & Regards Ravi Shankar

    Wednesday, August 13, 2014 8:39 AM