Dynamic Styles – part 2

In the last post we saw we could change the skin of the application without reloading the app. what is missing is when the user opens the app the next time, it doesnt remember the last theme selected. To start the app with the last selected theme when the user runs the app again is quite easy

We need to make the changes only in couple of places.

In Page.xaml.cs, before we clear the ApplciationSettings, we get the theme we stored, if it is there and after we load all our themes we default to the one we got from ApplicationSettings. When the user changes the Theme we save it in the ThemeChanged eventhandler.

The modified version of the code in Page.xaml.cs is below

 public Page()
            string currentTheme = “DefaultTheme”;
            if (IsolatedStorageSettings.ApplicationSettings.Contains(“CurrentTheme”))
                currentTheme = IsolatedStorageSettings.ApplicationSettings[“CurrentTheme”] as string;

            IsolatedStorageSettings.ApplicationSettings.Add(“CurrentTheme”, currentTheme);

            LayoutRoot.Background = App.Current.Resources[“BkgBrush”] as SolidColorBrush;

            ThemeSelection ts = new ThemeSelection(currentTheme);
            cc2.Content = ts;
            ts.ThemeChanged += new ThemeSelection.ThemeChangedEventHandler(ts_ThemeChanged);
            MainContent mc = new MainContent();
            cc1.Content = mc;

 void ts_ThemeChanged(object sender, ThemeChangedEventArgs e)
            ThemeSelection ts = new ThemeSelection(e.Theme);
            IsolatedStorageSettings.ApplicationSettings[“CurrentTheme”] = e.Theme;
            cc2.Content = ts;
            MainContent mc = new MainContent();
            cc1.Content = mc;
            ts.ThemeChanged += new ThemeSelection.ThemeChangedEventHandler(ts_ThemeChanged);
            LayoutRoot.Background = App.Current.Resources[“BkgBrush”] as SolidColorBrush;


With these changes in place, when the user runs the application, it shows up with the theme where he left the last time

3 thoughts on “Dynamic Styles – part 2

  1. When I read the title of this post, I thought you got the solution for Style write-once limitation.

    Unfortunately, the way that you mentioned in article is not about changing the style on the fly. It’s about saving the user preference in Isolated storage.

    Let me know if I’m in wrong direction. I would like to discuss more if you have the solution to eliminate the Silverlight Style Write-Once limitation.


  2. Michael,
    this was continuation of previous post where the theme(skin) chosen by the user is not retained with the subsequent visits hence I have given -part 2(that would mean part 2)

    I dont have solution to Style write one limitation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s