Using a server side Prism module catalog in Silverlight

Prism 4.0, Managed Extensibility Framework, and Silverlight 4.0 provide an ideal combination to develop robust and flexible enterprise applications. This article explains how you can keep the Prism module catalog in such an application on the server side. In most of the Prism Quickstarts, the module catalog is embedded in the shell: The application class starts the bootstrapper: private void Application_Startup(object sender, StartupEventArgs e) {     ApplicationBootstrapper bootstrapper = new ApplicationBootstrapper();     bootstrapper.Run(); } The bootstrapper builds the catalog from the local file: protected override IModuleCatalog CreateModuleCatalog() {     ModuleCatalog catalog = Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("Prism.Sample.Shell;component/Modules/ModuleCatalog.xaml", UriKind.Relative));     return catalog; } Unfortunately, this setup requires a rebuild and a redeploy of the shell application whenever a change in the catalog is needed (e.g. to add an extra module, or to remove a buggy module). In most -if not all- real-world scenarios it makes more sense to place the catalog in an area that allows easy modification. The following solution has the catalog in a file on the server side, but you can easily go for other storage locations, like a database table, a WCF service or a SharePoint list. Here's where the file is kept: The bootstrapper gets an extra constructor that takes a stream: private Stream stream;   public ApplicationBootstrapper(Stream stream) {     this.stream = stream; } This stream is used to build the catalog: protected override IModuleCatalog CreateModuleCatalog() {     ModuleCatalog catalog = Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(this.stream);     return catalog; } On application startup, the file is read, wrapped in a stream, and handed over to the bootstrapper: private void Application_Startup(object sender, StartupEventArgs e) {     WebClient client = new WebClient();     client.OpenReadCompleted += this.Client_OpenReadCompleted;     client.OpenReadAsync(new System.Uri(@"Modules\ModuleCatalog.xaml", System.UriKind.Relative)); }   private void Client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {     ApplicationBootstrapper bootstrapper = new ApplicationBootstrapper(e.Result);     bootstrapper.Run(); } The following two projects demonstrate both approaches. The application is a small Prism-MEF-Silverlight tutorial application that I wrote a couple of months ago. Here's how it looks like: Here's the project before and after the modification (it contains the Prism dll's, so you might need to unblock these): Catalog in the Shell xap: U2UConsult.Prism.Sample.Application.1.zip (1,99 mb) Catalog on the server: U2UConsult.Prism.Sample.Application.2.zip (1,03 mb) Enjoy !

Themebuilding in Silverlight

In this short article I'll show you how to transform a resource dictionary with colors, brushes, styles and templates into a real theme - à la Silverlight toolkit themes - and how to apply it to a region in the user interface. I'll build a theme called Glass. Building the theme Each Silverlight toolkit theme is nothing more than a resource dictionary wrapped in ... er ... a wrapper and compiled as a separate dll. I you want to roll your own, you first create a class library project. The project will contain lots of references, lots of XAML, and just a couple of lines of C#. It will eventually look like this: Create a resource dictionary named theme.xaml and stuff all the artefacts that your designer created - or that you downloaded somewhere - into it. Then create a very thin wrapper class around the resource dictionary. The class should inherit from the toolkit's Theme class: Just like in the toolkit themes, it makes sense to use System.Windows.Controls.Theming as namespace. Here's the whole code for that class: /// <summary> /// Implicitly applies the Glass theme to all of its descendent FrameworkElements. /// </summary> public partial class GlassTheme : Theme {     /// <summary>     /// Stores a reference to a Uri referring to the theme resource for the class.     /// </summary>     private static Uri ThemeResourceUri = new Uri("/System.Windows.Controls.Theming.Glass;component/Theme.xaml", UriKind.Relative);       /// <summary>     /// Initializes a new instance of the GlassTheme class.     /// </summary>     public GlassTheme()         : base(ThemeResourceUri)     {     } } We're done building the theme. Now it's time to test it. Applying the theme All you need to do to statically apply a theme to a region in the user interface, is dropping it in the XAML: <customtheme:GlassTheme>     <!-- Your controls here. --> </customtheme:GlassTheme>  To test a new theme, I created a small test application with controls from different sources: a handful of native Silverlight controls, some SDK controls and a pinch of Toolkit controls. It's actually a lightweight version of one of the zones from the Silverlight Samples page. Just select Theme Browser over there to get the full page. My version is a little more compact. It looks like this: With a simple right-click you can dynamically change the theme. Here's how the Glass theme looks like in the test container:  The test container also applies the selected theme through XAML, but it uses the common base class to set the appropriate ThemeUri: <toolkit:Theme x:Name="ThemedRegion"                ThemeUri="/System.Windows.Controls.Theming.Glass;component/Theme.xaml">     <!-- Your controls here ... --> </toolkit:Theme> After selection, a new theme is programmatically applied like this: themedRegion.ThemeUri = new Uri("/System.Windows.Controls.Theming." + this.themeName + ";component/Theme.xaml", UriKind.RelativeOrAbsolute); Source code Here's the glass theme and its test container: U2UConsult.Silverlight.ThemeBuilder.zip (2,34 mb) Enjoy!