Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Silverlight 4 – Implicit Styles and Themes

Blogs

Mike Taulty's Blog

Elsewhere

I hadn’t really thought properly about implicit styles in Silverlight 4 although I wrote a little about it back here and it’s something that WPF always had ( if I remember correctly ).

The bit that I hadn’t really thought about was how you could set up a set of implicit styles for all your controls, throw them into separate resource dictionaries and then just load one style or the other at runtime and push it into the MergedDictionaries property of your application’s ResourceDictionary. It’s fairly obvious so I don’t think I’m stating anything new but I just hadn’t really thought about it.

Also, of course, the Silverlight Toolkit already has this sort of theming support for both Silverlight 3 and 4.

You might load those resource dictionaries from embedded XAML files in your assembly or XAP or it might be from loose XAML files that you load from the file system or from the web.

As a simple example, below is an “app” which displays a few controls and then offers the chance to load one of 2 XAML files ( based on the ListBox selection ) from the site-of-origin in order to change the application’s theme.

The code’s pretty simple with the heart of it being something like this;

 void ChangeDictionary()
    {
      IsBusy = true;

      WebClient client = new WebClient();

      client.DownloadStringCompleted += (s, e) =>
        {
          if (!e.Cancelled && (e.Error == null))
          {
            try
            {
              ResourceDictionary dictionary = XamlReader.Load(e.Result) as ResourceDictionary;

              if (dictionary != null)
              {
                Application.Current.Resources.MergedDictionaries.Clear();
                Application.Current.Resources.MergedDictionaries.Add(dictionary);
              }
            }
            catch (XamlParseException)
            {
            }
            finally
            {
              IsBusy = false;
            }
          }
        };

      client.DownloadStringAsync(new Uri(selectedDictionaryFile, UriKind.Relative));
    }

which just sets a busy flag ( for a BusyIndicator control ) and then downloads the XML file containing the ResourceDictionary asynchronously and slots it into the application’s resources.

The code’s here for download.


Posted Mon, Feb 15 2010 5:31 PM by mtaulty
Filed under: ,

Comments

Delay wrote re: Silverlight 4 – Implicit Styles and Themes
on Mon, Feb 15 2010 7:53 PM

Mike,

I've got something very much along the lines of this ready for the next Silverlight 4 Toolkit. There's even a tweak to the above approach that makes things a little more seamless in some respects - though I like how you tied in the BusyIndicator. I'll blog about what it is and how to use it once the bits go public.

Thanks for the cool post!

Ed wrote re: Silverlight 4 – Implicit Styles and Themes
on Tue, Feb 16 2010 6:14 AM

The StylesTest project doesn't include Dictionary1.xaml an Dictionary2.xaml.

mtaulty wrote re: Silverlight 4 – Implicit Styles and Themes
on Tue, Feb 16 2010 7:33 AM

Ed,

Sorry - those files aren't needed. They're not missing, just delete them from the project and things should be fine.

===

Delay,

Thanks for reading - "we're not worthy!" :-)

Mike.

Russell wrote re: Silverlight 4 – Implicit Styles and Themes
on Tue, Feb 16 2010 12:05 PM

Hi Mike

In your example you do an Application.Current.Resources.MergedDictionaries.Clear();

This assumes you only have 'themed' merged dictionaries. If you had other merged dictioaries is it better to clear them all and reload or remove and add just the 'themed' ones? I assume the second, so what is the best way of identifying the merged dictionary to remove, Application.Current.Resources.MergedDictionaries[index].Source.OriginalString?

Thanks

Russell

p.s. What happened to the beer and pizza evenings at Microsoft, Reading? I used to enjoy those and with all the new stuff coming out of Microsoft these day, they are sorely missed.

Muaz Zubair wrote re: Silverlight 4 – Implicit Styles and Themes
on Fri, Feb 26 2010 10:10 PM

Implicit styles are good feature in SL

I realy appriciate it.