Dynamic Styling

Dynamic Styling

Dynamic Theming in Silverlight (Dynamic Styling in Silverlight)

 

Now, I have completed my first tutorial with a sample application to show you all about Dynamic Theming or to make it simple, Change the style of a Silverlight Application dynamically. As I said, this is my tutorial and there will be things that need to be corrected. I request all the experienced pals to please comment on this, so that I can improve the standard on the next post. Hope this will be good example for the newbie’s, and this will be helpful as well;

 

I have been researching in this for quite a while and was unable to find a good article on this. At last I got some good stuff, on the subject of this and was able to create an application based on this. I am posting this for the reference.

 

This application has a Style by default and this is mandatory. Style can be changed at runtime with one condition, the application will reload (not refresh), and so if there is something to be saved, it has to be saved temporarily to a variable / public class. What I mean by reload is, I will clear the root visual of the application and add it once again. I don’t think this will impact the development of the application in a smoother approach and this can be straight forward.

So let’s ditch into the application.

 

A style needs to be created in the Silverlight application and this has to be referred / merged to the application.

e.g.:

 

<Application.Resources>

         <ResourceDictionary>

                 <ResourceDictionary.MergedDictionaries>

                          <ResourceDictionary Source="Assets/CommonStyles.xaml"/>

                 </ResourceDictionary.MergedDictionaries>

         </ResourceDictionary>

</Application.Resources>

 

All the templates/styles/brush which is there in the file in the client, i.e. Merged Resource dictionary will be replaced by the styles/templates/brush placed in the XAML file in the server, which has to load dynamically; the theme / style file is loaded with the help of the web client class; WebClient is a class that helps to download files from a location/URL.

 

Apart from this, I’ve included a way to pass the init parameters, which is encoded from the ASPX and decoded in the XAML; this will be a very useful thing as Encode will suppress all the special characters.

 

The Converters will work, only if the DataContext of a page is set; since I am not using the MVVM pattern, and I don’t have any specific DataContext, I am setting the DataContext of the Main Page as Main Page itself;

 

this.DataContext = this;

 

Now, let me explain the application that has a dynamic styling.

 

Style Files are created and placed it in the server; when it is required, it is then added / merged to the resource dictionary of the application (APP.XAML)

 

As you can see in the above code, the application already has a Resource Dictionary merged within the resources.

 

ResourceDictionary Source="Assets/CommonStyles.xaml"/>

 

This has to be removed before you add any other resource dictionary, since the Keys used for the style will be the same; I am loading the theme as a resource dictionary

 

Check this:

 

public static void LoadResourceDictionary(string xamlTheme)

        {

            if (string.IsNullOrEmpty(xamlTheme))

            {

                return;

            }

 

            try

            {

                using (StringReader stream = new StringReader(xamlTheme))

                {

                    Grid rootVisual = Application.Current.RootVisual as Grid;

                    ResourceDictionary theme = XamlReader.Load(stream.ReadToEnd()) as ResourceDictionary;

                    Application.Current.Resources.MergedDictionaries.Clear();

                    Application.Current.Resources.MergedDictionaries.Add(theme);

 

                    rootVisual.Children.Clear();

                    rootVisual.Children.Add(new MainPage());

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show("Failed to load the theme \n" + ex.Message);

            }

        }

 

I don’t think, adding the main page again is an issue, as we always have an option to save the data to a common class.

 

There are some common methods, and constants, which can be added in a separate project (Class Library) and can be used in all the projects, All those common things comes under My.Common namespace

 

Click here to download the sample application; the coding style is of Microsoft Standard (As read in the articles) and I have tried to comment on all the methods and properties for better understanding;

 

Any questions, just ask;

 

You can reach me at

Email         : neal.gabriel@live.com

Silverlight   : neal.gabriel (which will redirect to my live.com ID)

Skype         : neal.gabriel

GTalk         : neal.gab

Twitter       : @nealgabriel

 

Conclusion

 

I am not sure, whether the article is too short or difficult to understand. As this is my first real article, I request you to please comment on this so that I can improve.

Comments