说明

使用的安装包有:

  • Prism 6.3
  • Unity 4.0.1

基于Prism框架的应用程序都包含一个主项目和若干功能模块,主项目负责启动时初始化工作,包括依赖注入容器,定义Shell等等。功能模块则负责单独的逻辑功能。下面使用helloworld说明。

1. 安装

新建WPF项目,使用4.5 版 .NET Framework。安装如下安装包。

Install-Package Prism.Unity

2. 编写主项目

2.1 定义Shell

删除MainWindow.Xaml文件,并新增Shell.Xaml文件。Shell是主界面,包含一个多个域(Region),每个域是一个容器,可存放一个或多个自定义组件,这里使用ItemsControl实现。定义域时需要指明域名称。

xaml
<Window x:Class="HelloWorld.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
Title="Hello World" Height="300" Width="300">
<ItemsControl Name="MainRegion" prism:RegionManager.RegionName="MainRegion" />
</Window>
behind code
using System.Windows;

namespace HelloWorld
{
/// <summary>
/// Interaction logic for Shell.xaml
/// </summary>
public partial class Shell : Window
{
public Shell()
{
InitializeComponent();
}
}
}
2.2 创建Bootstrapper

所谓Bootstrapper可以简单理解为创建Prism项目的引导程序,该引导程序需要做多项工作,这里不详细讲述。BootStrapper初始化。

using System.Windows;
using Microsoft.Practices.Unity;
using Prism.Unity; namespace HelloWorld
{
class HelloBootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return this.Container.Resolve<Shell>();
} protected override void InitializeShell()
{
base.InitializeShell(); App.Current.MainWindow = (Window)this.Shell;
App.Current.MainWindow.Show();
} protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
}
}
2.3 启动Bootstrapper

在App.xaml.cs中重写程序启动事件处理句柄,在原有处理上追加实例化Bootstrapper,启动。

public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
}

3. 模块化开发

Prism最大好处就是模块化开发,为此需要先定义一个模块。

3.1 定义功能模块

每一个模块包含一个或多个View,为了演示方便这里定义一个view。步骤:

  • 新增一个HelloWorldModule项目
  • 安装Prism.Unity包
  • 在新增项目新建Views文件夹,并在Views文件夹中添加HelloWorldView.Xaml文件
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">
<Grid>
<TextBlock Text="Hello World" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"></TextBlock>
</Grid>
</UserControl>
behind code
namespace HelloWorldModule.Views
{
/// <summary>
/// Interaction logic for HelloWorldView.xaml
/// </summary>
public partial class HelloWorldView : UserControl
{
public HelloWorldView()
{
InitializeComponent();
}
}
}

3.2 模块封装

定义好view以后我们需要将Views封装为一个模块,方法是定义模块 类并实现接口IModule。

using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions; namespace HelloWorldModule
{
public class HelloWorldModule : IModule
{
private readonly IRegionViewRegistry regionViewRegistry; public HelloWorldModule(IRegionViewRegistry registry)
{
this.regionViewRegistry = registry;
} public void Initialize()
{
//将view注册在MainRegion域
regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(Views.HelloWorldView));
}
}
}

4. 导入模块

前面定义的模块是独立项目,为了让主项目找到该模块,需要将独立模块导入主项目中,方法是在模块目录添加该模块,Prism会找到该模块。在bootstrapper中使用如下代码导入。

//添加命名空间
using Microsoft.Practices.Prism.Modularity;
//将模块注册至模块目录
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog(); ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
moduleCatalog.AddModule(typeof(HelloWorldModule.HelloWorldModule));
}

最新文章

  1. ZOJ 3911 线段树
  2. 测试Swift语言代码高亮-使用highlight.js
  3. c programming language ___ 5_2.c
  4. C# winform 最小化到电脑右下角
  5. pdf增加水印
  6. 【转】 ubuntu12.04更新源
  7. eclipse中运行tomcat找不到jre的解决办法
  8. 从汇编看c++中指向成员变量的指针(二)
  9. 面试之Java知识整理
  10. 《Django By Example》第十二章 中文 翻译 (个人学习,渣翻)
  11. X5 Blink下文字自动变大
  12. JS中for循环变量作用域
  13. BugPhobia回顾篇章:团队Beta 阶段工作分析
  14. 自学工业控制网络之路1.2-典型的现场总线介绍PROFIBUS
  15. Scala_控制结构
  16. HDU 3371 Connect the Cities(prim算法)
  17. centos 7 安装搜狗输入法
  18. SpringBoot(三):文件下载
  19. HUST 1599 - Multiple(动态规划)
  20. ArcMap2SLD使用

热门文章

  1. linux c 多线程开发
  2. ansible-playbook安装tomcat
  3. Python+Appium自动化测试(9)-自动选择USB用于传输文件(不依赖appium对手机页面元素进行定位)
  4. 2014年 实验二 B2C网上购物
  5. MeteoInfoLab脚本示例:获取气团轨迹每个节点的气象数据
  6. git commit 代码提交规范
  7. zookeeper 集群搭建 转
  8. centos8上配置openresty/nginx可访问php
  9. django—视图相关
  10. OpenCV(c++)-1 安装和配置OpenCV4.4(Windows+visual studio 2019)