我的第一个《hello word 》 prism+silverlight 程序

准备工作:

需要引用的类库:

using Microsoft.Practices.Prism.Regions;

using Microsoft.Practices.Prism.UnityExtensions;

 

 

步骤:

一:创建一个silverlight 工程,可以选择已有的web作为展示网站;

 

二:创建Shell

     删除MainPage.xaml,创建新的silverlight 用户控件Shell.xaml;

     在shell命名空间上面添加引用: xmlns:prism="http://www.codeplex.com/prism" 

     并删除页面上默认的grid布局;添加如下代码:<ItemsControl Name="MainRegion" prism:RegionManager.RegionName="MainRegion" />

    修改后shell.xaml 代码为:         

<UserControl x:Class="HelloWordSilverlight.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://www.codeplex.com/prism"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

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

三:添加Bootstarpper:在该类中创建shell类,并将该shell视图显示出来。

      在程序中使用框架必然要有一个切入点,框架会在这里进行初始化,处理相关配置信息等。在Prism中扮演这一角色的就是Bootstrapper。

      在HelloWorld.Desktop项目中添加一个名为Bootstarpper的类,这里覆写了CreateShell方法,以创建一个Shell窗体对象并显示:

     注意添加引用:using Microsoft.Practices.Prism.UnityExtensions;                                

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.UnityExtensions;
namespace HelloWordSilverlight
{
public class Bootstarpper:UnityBootstrapper
{
        protected override DependencyObject CreateShell()
         {
               Shell shell = new Shell();
               return shell;
         }
}

//初始化shell 注意如果没有这个函数,页面将不会显示

protected override void InitializeShell()
{

base.InitializeShell();
Application.Current.RootVisual = (UIElement)this.Shell;

// Application.Current.MainWindow.Show();

}

}

4.修改app.xaml文件

       在App.xaml文件里,删除Application标签中的:StartupUri="Window1.xaml"。在相应的后台文件App.xaml.cs中修改Application_Startup方法:

       注释: this.RootVisual = new MainPage();

       并填写:在程序运行时,生成并调用Bootstarpper 类,

Bootstarpper bootshaper = new Bootstarpper();
bootshaper.Run();

5. 创建一个名为HelloWorldModule的类库,添加对Prism类库Microsoft.Practices.Prism的的引用:

       using Microsoft.Practices.Prism.Regions;

        删除自动生成的class1.css

6.在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"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"> HELLO WORD !</TextBlock>
</Grid>
</UserControl>

 

7.在HelloWorldModule类库中添加一个名为HelloWorldModule的类,它派生自IModule接口,为此我们要实现它的Initialize方法: 

       通过这个接口,来将我们module中的某些视图 注册成某些上层项目所调用的regionname;

       Microsoft.Practices.Prism.Modularity;

       

      我们发现,在Initialize方法中,将前面第3步指定的MainRegion与文件夹Views中HelloWorldView类(这是一个xaml)的相关联。

      同时,这里提及一句,我们使用了依赖注入(构造函数)的方式,来传递参数regionViewRegistry。

 8.返回到HelloWorld.Desktop应用程序中,添加对HelloWorldModule类库的引用。 

9.最后,返回到第3步创建的Bootstarpper中,我们还要覆写它的GetModuleCatalog方法,从而找到并加载所指定的模块:

//最后一步工作,将model加入显示页面
protected override IModuleCatalog CreateModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog()
.AddModule(typeof(HelloWorldModule.HelloWorldModule));
return catalog;
//return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("/MyFirstPrism_Silverlight;component/ModulesCatalog.xaml"));
}

 最终整理后的bootstarpper 如下图:

                 

 

          

    

posted @ 2015-08-27 20:34  梅花香自苦寒来_rainy  阅读(204)  评论(0)    收藏  举报