This article describes the methods of composite application building with native Silverlight tools. Project examples are attached. It also explains the need to create separate Application inherited class named ScabApplication.

Creating Modules

Modules are deployment units in composite application. Developers can separate areas of their application into different modules to be able to deploy them to different users or applications. We use the Loaded event handler to make the module initialization because Application.Current is null in Application.Resources element constructor.

To create a module

  1. Create a new Silverlight Class Library
  2. Create a new public class in the library project. Inherit it from System.Windows.Controls.Panel (or other FrameworkElement derived class) having Loaded event.
  3. Declare any public properties that can be used for module configuration.
  4. In the public class constructor subscribe itself to Loaded event.
  5. Implement the initialization logic in the Loaded event handler. For example you can add services programmatically: Application.Current.Resources.Add("ModuleService", new SimpleService())

To load a module

  1. Declare in the App.xaml xmlns referring to module assembly and namespace. For example xmlns:modules="clr-namespace:Modules;assembly=Modules"
  2. In Application.Resources list the appropriate modules to load as shown in the following template:
    <application.resources>
      <modules:Module x:Key="Module">
        <modules:Module x:Key="SubModule" ServiceName="SubService"/>
      </modules:Module>
    </application.resources>

Please note you can declare modules hierarchically with custom parameters declared in module classes. In some cases App.xaml can be excluded from assembly and included into Xap. It allows application to be configured on server side.

Example

Modules.zip (48.63 kb)

The following example includes CompositeApplication application and Modules module assemblies. Modules assembly declares Module class with custom parameter ServiceName. Module class declares Loaded event handler named Initialize and registers SimpleService service in Application.Resources that does not make anything.

App.xaml file in application assembly refers to Modules assembly by xmlns:modules and specifies to load modules in Application.Resources. The submodule will be loaded with ServiceName="SubService" parameter.

Creating a Shell

There are two methods to create shell in composite application. The main disadvantages of pure Silverlight approach without extra application level coding:

  • We cannot manage the order in which modules and shell are initialized. Application Startup runs before any module Loaded events.
  • We cannot create shell Loaded event handlers from modules. Thus it is impossible to access shell from modules.
  • We cannot create shell from module. Because Silverlight does not call modules Loaded events if RootVisual was not created.

To create shell from application

  1. Create a new Silverlight Application
  2. This is the first method of shell creation :). Shell is the Page class that initialized in App.xaml.cs Application_Startup method.

To create shell like a module

  1. Create a new Silverlight Class Library project. It will contain your shell.
  2. Create new Silverlight User Control in the module assembly. It is a shell.
  3. Create a new Silverlight Application.
  4. In App.xaml declare xmlns referring to shell in shell assembly and add the shell into Application.Resources with predefined x:Key value like this:
    <Application.Resources>
      <shellmodule:Shell x:Key="Shell" />
    </Application.Resources>
  5. Replace the content of App.xaml.cs Application_Startup method with RootVisual creating from Application.Resources referring by predefined x:Key. For example:
    this.RootVisual = (UserControl)Resources["Shell"];

Example

Shell.zip (52.55 kb)

The following example contains assembly and shell module projects. Shell module declares Shell UserControl class with TextBlock "Shell". App.xaml declares Shell in Application.Resources with x:Key="Shell". Then application specifies the source for RootVisual as "Shell" from Application.Resources.

Conclusions

  • The above mentioned disadvantages make it impossible to register services and MVC elements in modules and use them in Shell. We can't access the Application before shell will be loaded. Thus we cannot register global objects like Services, Viewers, Controls from modules and use them in Shell.
  • ScabApplication class is required to provide the possibility of module initialization in predefined order before and after the shell creation.

Modules.zip (48.63 kb)

Shell.zip (52.55 kb)

Currently rated 1.5 by 2 people

  • Currently 1.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5