In this four part series based on Microsoft Patterns and Practices guidance, I am going to build a Composite Silverlight application using Prism. This tutorial is in both C# and Visual Basic, but when creating projects in Visual Studio, the images I may use may be C# templates for example, but you should be able to do exactly the same in Visual Basic and vica-versa. It avoids the repetition of posting two images with “Open C# Silverlight Application” and “Open Visual Basic Application”, when the Visual Studio templates are the same – bar the language. I will however, post code samples in both languages
Configuration
You will need the Silverlight development tools, and the Prism Libraries. For additional configuration information you can use the codeplex site.In the project you will need to reference the Prism Libraries, I downloaded the CompositeApplicationGuidance-Feb2009.exe from the link above, and extracted the files into a folder. You should now have the following files
Take the time to read the “Readme.txt”. You will also need to open the “Open Composite Application Library.bat” and build that project in Visual Studio. It is only after building the solution, that the requisite libraries become available in CompositeApplicationGuidance-Feb2009CALSilverlightComposite.UnityExtensionsBinDebug.
CAB Silverlight Demo
Create a new Silverlight application and call the Solution NewsAggregator and the project NewsAggregator.Shell
Create an ASP.NET Web Project
In the Shell Project, delete the page.xaml and add the shell right clicking the project in solution explorer and choose Add a new item
Add a Silverlight User Control and call this Shell.xaml
Now you need to connect this application to Prism, and you do this typically using the “Bootstrapper”. To do this, create a new class called Bootstrapper and add it to the project, and inherit from UnityBootstrapper. UnityBootstrapper is available in the Prism libraries your require, so you will need to add references to them.
In C# right click the add a reference node and chose Add Reference
In Visual Basic double click your MyProject node in solution explorer and navigate to the references tab, and choose Add Reference. You should then navigate to the folder with the libraries (see configuration section above) and add the following five libraries
There are two things required by the bootstrapper, one of them is the Shell and the other is the ModuleCatalog
Visual Basic
Imports Microsoft.Practices.Composite.UnityExtensions
Imports Microsoft.Practices.Composite.Modularity
Public Class Bootstrapper
Inherits UnityBootstrapper
Protected Overrides Function CreateShell() As System.Windows.DependencyObject
Return New Shell
End Function
Protected Overrides Function GetModuleCatalog() As Microsoft.Practices.Composite.Modularity.IModuleCatalog
Return New ModuleCatalog
End Function
End Class
C#
using System.Windows;
using Microsoft.Practices.Composite.UnityExtensions;
using Microsoft.Practices.Composite.Modularity;
namespace NewsAggregator.Shell
{
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return new Shell();
}
protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
{
return new ModuleCatalog();
}
}
}
For Silverlight applications you need to set the RootVisual, which is the starting control for a Silverlight application.
Visual Basic
Protected Overrides Function CreateShell() As System.Windows.DependencyObject
Dim shell As New Shell
Application.Current.RootVisual = shell
Return shell
End Function
C#
protected override DependencyObject CreateShell()
{
Shell shell = new Shell();
Application.Current.RootVisual = shell;
return shell;
}
You then need to connect the Bootstrapper to the application, to do this, double click the App.xaml.cs or App.xaml.vb depending on your language and set the RootVisual in the Application Startup method thus
Visual Basic
Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
Dim bootstrapper As New Bootstrapper
bootstrapper.Run()
End Sub
C#
private void Application_Startup(object sender, StartupEventArgs e)
{
new Bootstrapper().Run();
}
This will connect the application to Prism. Now everything is in place, we now can add a new module. Add a new Silverlight Class Library and call it NewsAggregator.Digg
Delete the default class1 that is added, and add references to the five Prism libraries (same libraries added to bootstrapper above).
add a new class to this project, and call it DiggModule and ensure it implements IModule
Visual Basic
Imports Microsoft.Practices.Composite.Modularity
Public Class DiggModule
Implements IModule
Public Sub Initialize() Implements Microsoft.Practices.Composite.Modularity.IModule.Initialize
End Sub
End Class
C#
using Microsoft.Practices.Composite.Modularity;
namespace NewsAggregator.Digg
{
public class DiggModule : IModule
{
#region IModule Members
public void Initialize()
{
//throw new NotImplementedException();
}
#endregion
}
}
Now that the module has been created, it needs to be added to the catalogue. This catalogue was created in the Bootstrapper, but that was just an empty catalogue. In the Shell project, add a reference to the Digg project, and set the method in the Bootstrapper
Visual Basic
Protected Overrides Function GetModuleCatalog() As Microsoft.Practices.Composite.Modularity.IModuleCatalog
Dim catalog As New ModuleCatalog
catalog.AddModule(GetType(DiggModule))
Return catalog
End Function
C#
protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog();
catalog.AddModule(typeof(DiggModule));
return catalog;
}
If you run the project you will get a message box asking if you want to enable debugging
click OK, but remember that this option must be set to false in production websites, and you now have an empty project that has loaded the Digg module
In lesson two we will focus on visual composition.
The complete source code for the 4 part series is available here (Download the WPF Silverlight Prism Folder)
Pingback: Building a Composite WPF and Silverlight Application with Prism - Part 2 « Ira Lukhezo’s blog
Pingback: Building a Composite WPF and Silverlight Application with Prism - Part 3 « Ira Lukhezo’s blog
Pingback: Building a Composite WPF and Silverlight Application with Prism - Part 4 « Ira Lukhezo’s blog