Silverlight Prism Hand On Lab

转载:http://dev.mjxy.cn/a-Silverlight-Prism-Hand-On-Lab.aspx

使用Prism开发Silverlight

1.新建Silverlight项目HelloWorld.Silverlight

2.更改MasterPage.xaml为Shell。注意类型

3.在Shell中引用

xmlns:Regions="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation"

4.删除Grid控件,添加ItemControl控件,并为其指定Regions属性

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

5.在项目中新建一个类Bootstrapper继承自UnityBootstrapper

引用命名空间

using Microsoft.Practices.Composite.Modularity;

using Microsoft.Practices.Composite.UnityExtensions;

public class Bootstrapper :UnityBootstrapper

6.重载实现UnityBootstrapper的CreateShell方法

protected override DependencyObject CreateShell()

        {

            Shell shell = Container.TryResolve<Shell>();

            Application.Current.RootVisual = shell;

            return shell;

        }

7.重载GetModuleCatalog方法,创建一个模块目录并返回目录接口。

 protected override IModuleCatalog GetModuleCatalog()

        {

            ModuleCatalog cataLog = new ModuleCatalog();

            return cataLog;

        }

8.打开App.xaml.cs创建启动

private void Application_Startup(object sender, StartupEventArgs e)

        {

            Bootstrapper bootstrapper = new Bootstrapper();

            bootstrapper.Run();

        }

9.编译运行

10.添加模块,Modules在Composite 中表示功能比较独立的一个逻辑单位

为解决方案添加Silverlight class library项目名称为HelloWorldModule并未项目添加引用:

Microsoft.Practices.Composite.dll

Microsoft.Practices.Composite.Presentation.dll

11.重命名class1为HelloWorldModule或新建,继承IModule接口。IModule接口在using Microsoft.Practices.Composite.Modularity;命名空间

12.实现IModule接口方法

public void Initialize()

        {

           

        }

13.增加新的文件夹

Views 存放视图

Services 存放服务接口和实现

Controllers 存放控制

14.填充模块

为HelloWorld.Silverlight添加HelloWorldModule项目引用,在GetModuleCatalog方法中调用AddModule方法添加HelloWorldModule模块

protected override IModuleCatalog GetModuleCatalog()

        {

            ModuleCatalog cataLog = new ModuleCatalog()

                .AddModule(typeof(HelloWorldModule.HelloWorldModule));

            return cataLog;

        }

15.为HelloWorldModule添加试图View

在View文件夹添加视图HelloWorldView.xaml

<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 Text="Hello World" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"></TextBlock>

    </Grid>

</UserControl>

16.在HelloWorldModule.cs中添加IRegionManager接口

private readonly IRegionManager regionManager;

17. 为HelloWroldModule添加构造函数,初始化接口

 public HelloWorldModule(IRegionManager regionManager)

        {

            this.regionManager = regionManager;

        }

18.在Initialize()中使用RegisterViewWidthRegion注册试图HelloWorldView

public void Initialize()

        {

            regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.HelloWorldView));

        }

19.编译运行。

posted @ 2011-07-12 01:09  敏捷学院  阅读(252)  评论(0编辑  收藏  举报