本篇将指导使用Prism框架创建一个WPF版本的Hello World程序。Silverlight版本的程序大同小异,不再敷述。
相关内容请参见文档P127-147,代码示例参加Quickstarts\ Hello World目录。
1.首先我们要准备Prism框架的6个dll,分别是:
· Microsoft.Practices.Composite.dll
· Microsoft.Practices.Composite.Presentation.dll
· Microsoft.Practices.Composite.UnityExtensions.dll
· Microsoft.Practices.ObjectBuilder2.dll
· Microsoft.Practices.ServiceLocation.dll
· Microsoft.Practices.Unity.dll
你可以从LIB目录中直接找到这些dll,也可以编译CAL目录中的项目,然后在bin\debug目录下找到它们。将它们复制到一个目录下,以便我们可以在多个项目中方便地进行使用。同时,大家会看到很多xml文件,它们的名称和这些dll是相同的——最好把它们也一起复制过去,这些xml文件是用来在Visual Studio中做智能感知(IntelliSence)的。
2.创建Shell
创建一个名为HelloWorld.Desktop 的WPF应用程序。添加对上述所有dll的引用。然后删除默认的Window1.xaml,新建一个Shell.xaml。
3.添加Region
在Shell.xaml中添加对Prism命名空间的引用,如下所示:
xmlns:cal="http://www.codeplex.com/CompositeWPF"
:cal="http://www.codeplex.com/CompositeWPF"
这样我们就可以为Region添加Prism中的依赖属性了。
删除Grid标签,替换以ItemsControl,并将其命名为MainRegion:
<ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />
ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />
这里cal:RegionManager.RegionName是一个依赖属性,我们将它与ItemsControl控件相关联。
修改后的Shell.xaml如下:
<Window x:Class="HelloWorld.Desktop.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.codeplex.com/CompositeWPF"
Title="Shell" Height="300" Width="300">
<ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />
</Window>
Window x:Class="HelloWorld.Desktop.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.codeplex.com/CompositeWPF"
Title="Shell" Height="300" Width="300">
<ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />
</Window>
4.添加Bootstarpper
在HelloWorld.Desktop项目中添加一个名为Bootstarpper的类,这里覆写了CreateShell方法,以创建一个Shell窗体对象并显示:
class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
Shell shell = new Shell();
shell.Show();
return shell;
}
}
Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
Shell shell = new Shell();
shell.Show();
return shell;
}
}
*注:这里要添加对一个命名空间的引用,才能使用UnityBootstrapper:
using Microsoft.Practices.Composite.UnityExtensions;
Microsoft.Practices.Composite.UnityExtensions;
5.修改App.xaml文件。
在App.xaml文件里,删除Application标签中的:StartupUri="Window1.xaml"。在相应的后台文件App.xaml.cs中覆写OnStartup方法:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
6. 创建一个名为HelloWorldModule的类库,添加对Prism类库的其中2个dll的引用:
· Microsoft.Practices.Composite.dll
· Microsoft.Practices.Composite.Presentation.dll
删除自动生成的class1.cs文件。
7.在HelloWorldModule类库中创建一个文件夹,取名为Views,在这个文件夹中创建一个HelloWorldView.xaml,我们为这个xaml添加一个显示HelloWorld的TextBlock:
<UserControl x:Class="HelloWorldModule.Views.HelloWorldView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Grid>
<TextBlock Text="Hello World" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"></TextBlock>
</Grid>
</UserControl>
UserControl x:Class="HelloWorldModule.Views.HelloWorldView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Grid>
<TextBlock Text="Hello World" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"></TextBlock>
</Grid>
</UserControl>
8. 在HelloWorldModule类库中添加一个名为HelloWorldModule的类,它派生自IModule接口,为此我们要实现它的Initialize方法:
public class HelloWorldModule : IModule
{
private readonly IRegionViewRegistry regionViewRegistry;
public HelloWorldModule(IRegionViewRegistry registry)
{
this.regionViewRegistry = registry;
}
public void Initialize()
{
regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(Views.HelloWorldView));
}
}
HelloWorldModule : IModule
{
private readonly IRegionViewRegistry regionViewRegistry;
public HelloWorldModule(IRegionViewRegistry registry)
{
this.regionViewRegistry = registry;
}
public void Initialize()
{
regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(Views.HelloWorldView));
}
}
我们发现,在Initialize方法中,将前面第3步指定的MainRegion与文件夹Views中HelloWorldView类(这是一个xaml)的相关联。
同时,这里提及一句,我们使用了依赖注入(构造函数)的方式,来传递参数regionViewRegistry。
9.返回到HelloWorld.Desktop应用程序中,添加对HelloWorldModule类库的引用。
10.最后,返回到第4步创建的Bootstarpper中,我们还要覆写它的GetModuleCatalog方法,从而找到并加载所指定的模块:
protected override IModuleCatalog GetModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog()
.AddModule(typeof(HelloWorldModule.HelloWorldModule));
return catalog;
}
IModuleCatalog GetModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog()
.AddModule(typeof(HelloWorldModule.HelloWorldModule));
return catalog;
}
*注:这里要添加对一个命名空间的引用,才能使用IModuleCatalog和ModuleCatalog:
using Microsoft.Practices.Composite.Modularity;
Microsoft.Practices.Composite.Modularity;这个例子的代码位于QuickStart中的HelloWorld目录中。
补充:
Prism是基于Silverlight 2.0版本的,但是由于它只是一个框架,所以对于当前的Silverlight 3 beta1版本也是适用的;而Prism中自带的几个Silverlight例子,则需要对sln文件进行转换才能打开,这里,我们只关心框架,而不care版本。
关于Silverlight的Hello World版本,也位于QuickStart中的HelloWorld目录中