INTRO
In my recent consulting assignment I was asked the question by one of the developer , that how can we get dynamic theming working in Prism application.My answer was that you can implement in the same manner as you do in the standard Silverlight application. But then he further asked that there are various regions in the RegionManager and how each views loaded in the different content regions can get unified theme , this encouraged me to try this out and see how it works.I started working on it and viola my answer was correct , there is no difference in implementing dynamic theming in prism specific app. In this blog post I explain you the same. I assume that readers are already aware of Prism library. If you are not then I strongly recommend that you acquire the knowledge of the same. In my example you will see the very basics of Prism app but the library has much more to offer.
GETTING STARTED
I created a very simple PRISM application which had only one Module (ModuleA). This module is loaded on demand when you click on the menu link which is under the ShellView.The application default gets loaded in the BlueTheme but it allows you to change the theme on the fly from the Themes menu. See the screen shot below of both themes.
The application has two content regions (MainContent and FooterContent).
- <menu:Menu Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top">
- <menu:MenuItem Header="Modules">
- <menu:MenuItem Header="Load Module A" Command="{Binding LoadModuleCommand}"/>
- </menu:MenuItem>
- <menu:MenuItem Header="Themes">
- <menu:MenuItem Header="Blue" Command="{Binding Path=ChangeThemeCommand}"
- CommandParameter="BureauBlue.xaml"
- IsChecked="{Binding IsBlueThemeSelected}"/>
- <menu:MenuItem Header="Black" Command="{Binding Path=ChangeThemeCommand}"
- CommandParameter="ExpressionLight.xaml"
- IsChecked="{Binding IsBlackThemeSelected}"/>
- </menu:MenuItem>
- </menu:Menu>
- <ContentControl prism:RegionManager.RegionName="MainContentRegion" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="12" />
- <ContentControl prism:RegionManager.RegionName="FooterContentRegion" Grid.Row="2" Margin="12"/>
Let me show you the code which does the magic of dynamic theming.
- public DelegateCommand<string> ChangeThemeCommand
- {
- get
- {
- if (_changeThemeCommand == null)
- {
- _changeThemeCommand = new DelegateCommand<string>(x =>
- {
- Application.Current.Resources.MergedDictionaries.RemoveAt(0);
- var theme = Application.GetResourceStream(new System.Uri("/Shell;component/Assets/" + x, System.UriKind.Relative));
- var rd = (ResourceDictionary)(XamlReader.Load(new StreamReader(theme.Stream).ReadToEnd()));
- Application.Current.Resources.MergedDictionaries.Add(rd);
- if (x == "BureauBlue.xaml")
- {
- IsBlueThemeSelected = true;
- }
- else
- {
- IsBlackThemeSelected = true;
- }
- }, y => _moduleLoaded);//only allow to change themes when module is loaded
- }
- return _changeThemeCommand;
- }
- }
The ChangeThemeCommand is DelegateCommand of String type. The command argument excepts the filename of theme. All theme files are located in Assets folders of the Shell project. The default theme is initialized in the App.xaml file with special syntax. It removes the current theme and loads the selected theme and merges it into the Resource Dictionary.
- <Application.Resources>
- <ResourceDictionary>
- <ResourceDictionary.MergedDictionaries>
- <ResourceDictionary Source="/Shell;component/Assets/BureauBlue.xaml" />
- </ResourceDictionary.MergedDictionaries>
- </ResourceDictionary>
- </Application.Resources>
This special syntax allows to access the files across different assemblies or xap files via relative paths.
CLOSURE
This was super simple to implement it and I highly recommend the devs to implement this feauture in their apps. Last a ninja tip. Always mark your assemblies in the module projects to CopyLocal=false if they are already referenced it in the Shell project. Download the sample code from here.
Pingback: Dynamic Theme Switching in Silverlight Prism App
Pingback: WindowsDevNews.com
What’s up friends, its fantastic piece of writing on the topic of cultureand fully explained, keep it up all the time.
Thanks
You actually have a point there, I have never considered it like it like
that before. You make it sound so stirring. I am going to have
to experiment with this more!