Geeks With Blogs
Keep It Simple Simple Solutions for Complex Scenarios

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

Posted on Thursday, July 30, 2009 3:17 AM Silverlight , NET Framework | Back to top


Comments on this post: How to Load Application Templates dynamically from Resources files

# re: How to Load Application Templates dynamically from Resources files
Requesting Gravatar...
Hola Juan sería bacán que actualizaras tu artículo para SL3 :

http://download.microsoft.com/download/1/2/E/12EF4026-9B38-4A29-9929-CB33B0EF87B1/ASPNETServerControlsforSilverlight.docx
Left by Gabo on Aug 01, 2009 2:14 PM

# re: How to Load Application Templates dynamically from Resources files
Requesting Gravatar...
Hola Gabo el codigo funciona en SL3, para eso tienes que descargar el proyecto de Codeplex el cual contiene el SilverlightControl que por defecto no viene en el SDK de SL3
Left by Juan on Sep 30, 2009 10:47 AM

# re: How to Load Application Templates dynamically from Resources files
Requesting Gravatar...
Very interesting topic. Thanks a million.
Left by Maria on Dec 22, 2009 8:18 AM

# re: How to Load Application Templates dynamically from Resources files
Requesting Gravatar...
didnt know that - thanks !
Left by גמילה מסמים on Jan 16, 2011 5:53 AM

Your comment:
 (will show your gravatar)


Copyright © Juan Mestas J. (aka gotchas) | Powered by: GeeksWithBlogs.net