Well on my last post, i talk about changes on controls in #Silverlight3, while i tried to use a demo about Navigation and Styles, Corrina and her team had developed 7 additional application templates, for the community to consume and they’ve put them up on the Expression Community Gallery for download:
Aurora
Frosted Cinnamon Toast
Lime Shocker
Pinky
Retro
Sky Line
Subdued
All templates are terrific and very easy to use only change the reference to the resource in the styles.xaml code file. But if we want work with all templates and load them, based on some policy or the user preference, in the same way that custom styles on ASP.Net? The easy way is get the template from the application host (ASP.Net), and pass it to Silverlight using the init parameters
Copy the styles.xaml files that you want use to the Assets folder in your project, rename each one of the files with a descriptive name, after define the logic for set the current theme in the Application Host (ASP.Net)
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5: using System.Web.UI;
6: using System.Web.UI.WebControls;
7:
8: namespace UIOne.Web
9: {
10: public partial class _Default : System.Web.UI.Page
11: {
12: protected void Page_Load(object sender, EventArgs e)
13: {
14: //Some sort of policy or user preference for get the current Theme
15: string applicationTheme = "Frosted.xaml";
16: this.Silverlight1.InitParameters = String.Format("applicationTheme={0}", applicationTheme);
17: }
18: }
19: }
Get the parameter in Silverlight code and rewrite the constructor of the Main class to receive one parameter
1: private void Application_Startup(object sender, StartupEventArgs e)
2: {
3: string applicationTheme = e.InitParams["applicationTheme"];
4: this.RootVisual = new MainPage(applicationTheme);
5: }
In the constructor of the Main class, get the resource file using the GetResourceStream method, create a StreamReader within the result and load the read content using the Load method from the XamlReader class
1: public MainPage(string applicationTheme)
2: {
3: string resurceUri = String.Concat("<<assembly>>;component/Assets/", applicationTheme);
4: StreamResourceInfo resourceInfo =
5: App.GetResourceStream(new Uri(resurceUri, UriKind.Relative));
6: StreamReader resourceReader = new StreamReader(resourceInfo.Stream);
7: string xaml = resourceReader.ReadToEnd();
8: ResourceDictionary resourceTheme =
9: XamlReader.Load(xaml) as ResourceDictionary;
10: App.Current.Resources.MergedDictionaries.Add(resourceTheme);
11:
12: InitializeComponent();
13: }
Now test the project, modifying the value for applicationTheme variable
See you