Partager via


How to: Create a Solution Using the Composite Application Library for Silverlight

Prerequisites

This topic requires you to have the following Composite Application Library and Unity Application Block assemblies:

  • Microsoft.Practices.Composite.dll
  • Microsoft.Practices.Composite.Presentation.dll
  • Microsoft.Practices.Composite.UnityExtensions.dll
  • Microsoft.Practices.Unity.dll
  • Microsoft.Practices.ServiceLocation.dll

The Composite Application Library ships as source code—this means you must compile it to get the Composite Application Library assemblies (Microsoft.Practices.Composite.dll, Microsoft.Practices.Composite.Presentation.dll, and Microsoft.Practices.Composite.UnityExtensions.dll).

To compile the solution

  1. In Windows Explorer, double-click the following batch file to open the Composite Application Library solution in Visual Studio:
    • Desktop & Silverlight - Open Composite Application Library.bat
  2. Build the solution. The Composite Application Library for Silverlight assemblies will be placed in the folder CAL\Silverlight\Composite.UnityExtensions\Bin\Debug.

Steps

To create a solution that uses the Composite Application Library, the following tasks must be performed:

  1. Create a solution with a Shell project.
  2. Define the Shell window layout.
  3. Set up the application's bootstrapper.
  4. (Optional) Create an infrastructure project.

The following procedure describes how to create a solution with a Shell project.

To create a solution with a Shell project

  1. In Visual Studio, create a new Silverlight application. To do this, point to New on the File menu, and then click Project. In the Project types list, click Silverlight in the Visual C# node. In the Templates box, click Silverlight Application. Finally, set the project's name, such as CALApplication1, specify a valid location, and then click OK.

    This project will be the Shell project of your application.

  2. On the Add Silverlight Application dialog box, make sure the Add a new ASP.NET Web project to the solution to host Silverlight option is selected, and then click OK.

  3. (Optional) In Windows Explorer, create a folder named Library inside your solution's folder, and then copy the following assemblies into the folder:

    • Microsoft.Practices.Composite.dll. This assembly contains the implementation of the Composite Application Library for Silverlight core components, such as modularity, logging and communication services, and the definitions for several core interfaces. This assembly does not contain user interface (UI)—specific elements.
    • Microsoft.Practices.Composite.Presentation.dll. This assembly contains the implementation of Composite Application Library for Silverlight components that target Silverlight applications, including commands, regions, and events.
    • Microsoft.Practices.Composite.UnityExtensions.dll. This assembly contains base and utility classes you can reuse in applications based on the Composite Application Library for Silverlight that consume the Unity Application Block. For example, it contains a bootstrapper base class, the UnityBootstrapper class, that creates and configures a Unity container with default services when the application starts.
    • Microsoft.Practices.Unity. This assembly enables you to use the Unity Application Block in your application. By default, applications built with the Composite Application Guidance use the Unity Application Block. However, developers who prefer to use different container implementations can build adapters for them using the provided extensibility points in the Composite Application Library.
    • Microsoft.Practices.ServiceLocation.dll. This assembly contains the Common Service Locator interface used by the Composite Application Guidance to provide an abstraction over Inversion of Control containers and service locators; therefore, the user can change the container implementation with ease.
  4. In the Shell project, add references to the assemblies listed in the preceding step.

Note

For IntelliSense to be available for the Composite Application Library, the XML documentation files for each of these assemblies must be placed in the same directory as the referenced assemblies.

The Shell window is a place to host different UI components; it exposes a way for itself to be dynamically populated by others. The following procedure explains how to define the Shell window layout.

To define the Shell window layout

  1. In Solution Explorer, rename the file Page.xaml to Shell.xaml.

  2. Open the code-behind file Shell.xaml.cs, and then rename the Page class to Shell using the Visual Studio refactoring tools. To do this, right-click Page in the class signature, point to Refactor, and then click Rename, as illustrated in Figure 1. In the Rename dialog box, type Shell in the New Name box, and then click OK. If the Preview Changes — Rename dialog box appears, click Apply.

    Ff921128.79ad4b0f-321b-495c-b67b-5ed2331ed430(en-us,PandP.20).png

    Figure 1

    Renaming Window1 using Visual Studio refactoring tools

  3. Open the Shell.xaml file in XAML view and set the following attribute values to the UserControl root element:

    • x:Class = "CALApplication1.Shell" (The Class attribute has to match the code behind class's name)

    Your code should look like the following.

    <UserControl x:Class="CALApplication1.Shell"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Name="Shell" Height="300" Width="300">
        <Grid>
    
        </Grid>
    </UserControl>
    

    The following steps describe how to add an ItemsControl control to the Shell window and associate a region to it.

  4. Open the Shell.xaml file.

  5. Add the following namespace definition to the root UserControl element. You need this namespace to use an attached property for regions; this property is defined in the Composite Application Library.

    xmlns:Regions="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation"
    
  6. Replace the Grid control in the Shell window with an ItemsControl control named MainRegion, as shown in the following code.

    <UserControl x:Class="CALApplication1.Shell"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Regions="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation"
        Height="300" Width="300">
        <ItemsControl Name="MainRegion" />
    </UserControl>
    
  7. In the ItemsControl control definition, set the attached property Regions:RegionManager.RegionName to "MainRegion", as shown in the following code. This attached property indicates that a region named MainRegion is associated to the control.

    <ItemsControl Name="MainRegion" Regions:RegionManager.RegionName="MainRegion" />
    

A Bootstrapper class is a class that is in charge of initializing the application. Typically, the bootstrapper initializes the container used for dependency injection, registers services and region adapters, creates and shows the Shell window, and loads modules. The following procedure explains how to set up the application's bootstrapper.

To set up the application's bootstrapper

  1. Add a new class file to the Shell project named Bootstrapper.cs.

  2. Add the following using statements at the top of the file.

    using Microsoft.Practices.Composite.UnityExtensions;
    using Microsoft.Practices.Composite.Modularity;
    
  3. Update the signature of the Bootstrapper class to inherit from the UnityBootstrapper class.

    public class Bootstrapper : UnityBootstrapper
    {
    }
    
  4. Override the CreateShell method in the Bootstrapper class. The CreateShell method should obtain an instance of the Shell window and display it to the user. The following code illustrates a possible implementation of the CreateShell method.

    protected override DependencyObject CreateShell()
    {
        Shell shell = Container.Resolve<Shell>();
        Application.Current.RootVisual = shell;
        return shell;
    }
    

    Note

    You can return the Shell object or null. If you return the Shell object, the UnityBootstrapper base class will attach the default region manager to it.

  5. Override the GetModuleCatalog method in the Bootstrapper class. In this template method, you typically create an instance of the module catalog, populate it with modules, and return it. The module catalog interface is Microsoft.Practices.Composite.Modularity.IModuleCatalog; it is responsible for containing the metadata of all the modules in the application. The Composite Application Library includes different methods of populating the module catalog in Silverlight: by creating the catalog in procedural code or by reading a XAML file. The following code illustrates how the Defining Modules In Code QuickStart implements the GetModuleCatalog method.

    Note

    To run this program the module types used in the code snippet below must be defined.

    protected override IModuleCatalog GetModuleCatalog()
    {
        ModuleCatalog catalog = new ModuleCatalog();
        catalog.AddModule(typeof (ModuleA), "ModuleD")
            .AddModule(typeof (ModuleB))
            .AddModule(typeof (ModuleD), "ModuleB")
            .AddModule(typeof (ModuleC), InitializationMode.OnDemand)
            ;
        return catalog;
    }
    

    Note

    For more information about how to create a module catalog and how to load modules in different scenarios, see How to: Populate the Module Catalog from Code and How to: Populate the Module Catalog from XAML.

  6. Open the file App.xaml.cs, and replace the Application_Startup event handler with the following code to initialize the bootstrapper during the application startup.

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        Bootstrapper bootstrapper= new Bootstrapper();
        bootstrapper.Run();
    }
    

    Note

    Additional customization can be made to the Bootstrapper class by overriding methods of its base class. For more information, see How to: Register and Use Services.

Additionally, you can create an infrastructure project to store elements shared by multiple modules in the application. The following procedure describes how to create an infrastructure project.

To create an infrastructure project

  1. In your solution, add a new Silverlight class library project named Infrastructure.
  2. Add references to the project to the following Composite Application Library assemblies:
    • Microsoft.Practices.Composite.dll
    • Microsoft.Practices.Composite.Presentation. dll
  3. To the Infrastructure project, add a reference in the Shell project.

Outcome

You will have a Composite Application Library for Silverlight solution that you can use as the base to build a composite application. This solution is the base required for multiple procedures in this guidance. Figure 2 illustrates the resulting solution in Solution Explorer.

Ff921128.a367c4ab-e1af-417c-bb43-bd2ea5f820e4(en-us,PandP.20).png

Figure 2

Resulting solution in Solution Explorer

Next Steps

The following are typical tasks that you perform after you create a solution using the Composite Application Library:

  • Modify the Shell layout. For example, you can add or remove regions, or you can change the overall appearance. For information about how to add a region, see How to: Add a Region.
  • Create modules. A module encapsulates a set of related concerns. Modules are independently developed and deployed, and they interact with each other to create the application. For information about how to create a module, see How to: Create a Module.

More Information

For a complete list of How-to topics included with the Composite Application Guidance, see Development Activities.

Home page on MSDN | Community site