Prism can be thought of as a set of libraries that help Silverlight applications be scalable and testable. It has a number of features (modularity, view regions and commanding) that help with this. This guide shows you how to get started writing a Prism application in Silverlight. It shows how to use the bootstrappers, modules, catalogs, regions and commanding.
<UserControl x:Class="PrismSample.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Regions="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation"
Width="400" Height="300">
<ItemsControl x:Name="MainRegion"
Regions:RegionManager.RegionName="MainRegion" />
</UserControl>
<ItemsControl x:Name="MainRegion"
Regions:RegionManager.RegionName="MainRegion" />
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
Shell shell = Container.Resolve<Shell>();
Application.Current.RootVisual = shell;
return shell;
}
protected override IModuleCatalog GetModuleCatalog()
{
}
}
public class ModuleAViewModel
{
public ModuleAViewModel()
{
Companies = new ObservableCollection<Company>();
SignalSomething = new DelegateCommand<object>(
executeCommand, canHandleButtonCommand);
}
public ObservableCollection<Company> Companies { get; set; }
public ICompanyService CompanyService { get; set; }
public DelegateCommand<object> SignalSomething
{
get;
set;
}
public bool canHandleButtonCommand(object o)
{
return true;
}
public void executeCommand(object o)
{
if (CompanyService != null)
{
Companies.Clear();
foreach (Company c in CompanyService.getCompanies())
Companies.Add(c);
}
}
}
public class CompanyService : ICompanyService
{
#region ICompanyService Members
public System.Collections.ObjectModel.ObservableCollection<Company> getCompanies()
{
return new Companies().List;
}
#endregion
}
<UserControl x:Class="ModuleA.Views.ModuleAView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
xmlns:my="clr-namespace:ModuleA.Views"
Width="400"
Height="300">
<UserControl.Resources>
<my:ModuleAViewModel x:Key="theViewModel" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot"
Background="White"
DataContext="{Binding Path=., Source={StaticResource theViewModel}}"
>
<ListBox Width="150"
Height="200"
ItemsSource="{Binding Companies, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="SimpleButton"
cal:Click.CommandParameter="SimpleButton"
cal:Click.Command="{Binding SignalSomething}"
HorizontalAlignment="Center"
VerticalAlignment="Bottom" />
</Grid>
</UserControl>
public ModuleAView(IUnityContainer container)
{
InitializeComponent();
ModuleAViewModel vm = LayoutRoot.DataContext as ModuleAViewModel;
vm.CompanyService = container.Resolve<ICompanyService>();
}
public class ModuleA : IModule
{
IRegionManager _regionManager;
IUnityContainer _container;
public ModuleA(IRegionManager regionManager, IUnityContainer container)
{
_regionManager = regionManager;
_container = container;
}
#region IModule Members
public void Initialize()
{
_container.RegisterType<ICompanyService, CompanyService>();
_regionManager.RegisterViewWithRegion("MainRegion", typeof(ModuleAView));
}
#endregion
}
protected override IModuleCatalog GetModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog();
catalog.AddModule(typeof(ModuleA.ModuleA));
return catalog;
}
This is the home of Prism. It will redirect to the MSDN code gallery to download Prism. Note: You do not need the project linker unless you're sharing code between WPF and Silverlight applications.
Interview with Bob Brumfield and David Hill of Microsoft's Patterns and Practices team.