先简单的介绍一下Prism框架,引用微软官方的解释:

Prism provides guidance to help you more easily design and build, flexible, and easy-to-maintain client business apps that run on Windows Runtime, Windows Presentation Foundation (WPF) desktop, Silverlight, or Windows Phone 7. These apps may start small and evolve over time.

官方链接https://msdn.microsoft.com/en-us/library/ff648465.aspx,可下载到文档和示例代码。

  多的介绍就不必了,研究这套框架的人基本是做WPF或者Silverlight的人,我是新人,理解不深还请大神指教。听说Prism是开源的,做了才一年的小菜目前框架都用的不熟,以后再看源码吧。

Prism要用到IOC容器,提供选择的有Unity和MEF,这个系列的博文以后我都选用MEF。不懂MEF的建议看看这位大牛的系列博文http://www.cnblogs.com/yunfeifei/p/3922668.html

先说一下思路:

  1、Prism思想是模块化编程,我将主界面拆分为四个模块(A、B、C、D)。

  2、模块之间不能互相引用,也就是解耦了。

  3、目前就以上两点,要考虑到以后对项目进行扩展,所以预留一个Infrastructure(Project)放置模块之间抽象的东西。

  完成之后的界面图如下:

  解决方案总体结构:

一、基本结构搭建

  1、按照上图结构添加项目,注意只有Desktop.MainWindow中保留App.xaml,并且输出类型是Windows应用程序,其余的Project都要删除App.xaml,输出类型为类库。

  2、删除Desktop.MainWindow中的默认的MainWindow,新建一个Shell窗体,根据Prism的思想,这个Shell就是主窗体。

  3、为所有的Project添加Prism的引用,我这里为所有的Project安装了以下四个Prism的Nuget包。

  

  4、在ModuleA中新建类ModuleA,ModuleB,ModuleC,ModuleD,继承自IModule接口,只有继承这个接口才能被加载进Prism中。

  

using Prism.Modularity;
using Prism.Mef.Modularity;
using Prism.Regions;
using System.ComponentModel.Composition; namespace ModuleA
{
[ModuleExport("ModuleA", typeof(ModuleA), InitializationMode = InitializationMode.WhenAvailable)]
public class ModuleA : IModule
{
private readonly IRegionViewRegistry regionViewRegistry;
[ImportingConstructor]
public ModuleA(IRegionViewRegistry registry)
{
this.regionViewRegistry = registry;
}
public void Initialize()
{
regionViewRegistry.RegisterViewWithRegion("RegionA",typeof(View.GridA));
}
}
}

注意最后一行代码:

regionViewRegistry.RegisterViewWithRegion("RegionA",typeof(View.GridA));
RegionA就是主界面吧Shell中为ModuleA模块预留的位置,可以理解为占位符,View.GridA就是我们为ModuleA写的的界面。BCD模块同理。

  5、添加一个BootStrapper类,这个类要继承自MefBootstrapper。

  Prism提供四种配置模块加载的方式,看官方文档(这里我提供第一种和第二种方式):

  

  

using Prism.Mef;
using Prism.Modularity;
using Prism;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ModuleA; namespace Desktop.MainWindow
{
public class BootStrapper:MefBootstrapper
{
protected override DependencyObject CreateShell()
{
return this.Container.GetExportedValue<Shell>(); } protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Shell)this.Shell;
Application.Current.MainWindow.Show();
} protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrapper).Assembly));
//this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(this.GetType().Assembly));
//第一种加载方式
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleA.ModuleA).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleB.ModuleB).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleC.ModuleC).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleD.ModuleD).Assembly));
} protected override IModuleCatalog CreateModuleCatalog()
{
// When using MEF, the existing Prism ModuleCatalog is still the place to configure modules via configuration files.
return new ConfigurationModuleCatalog();
}
//Prism提供四种加载模块的方式,以下是第二种从xaml文件中加载,注意文件要生成Resource,并且始终复制到输出目录
//protected override IModuleCatalog CreateModuleCatalog()
//{
// this.ModuleCatalog = new ModuleCatalog();
// var xamlCatalog = Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("ModuleCatalogs.xaml", UriKind.Relative));
// //var xamlCatalog = Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("ModuleCatalogs.xaml", UriKind.Relative));
// foreach (var item in xamlCatalog.Modules)
// {
// ModuleCatalog.AddModule(item);
// }
// return xamlCatalog; //}
}
}

  6、在App.xaml中把启动项设置为BootStrapper应该都会吧,不会的看源码哦。至此,一个简单的基于MEF的Prism框架就搭建好了。链接提供源码。

  源码在这呢

Prism for WPF初探(构建简单的模块化开发框架)的更多相关文章

  1. Prism for WPF 搭建一个简单的模块化开发框架 (一个节点)

    原文:Prism for WPF 搭建一个简单的模块化开发框架 (一个节点) 这里我就只贴图不贴代码了,看看这个节点之前的效果 觉得做的好的地方可以范之前的文章看看 有好的建议也可以说说   填充数据 ...

  2. Prism for WPF 搭建一个简单的模块化开发框架(六)隐藏菜单、导航

    原文:Prism for WPF 搭建一个简单的模块化开发框架(六)隐藏菜单.导航 这个实际上是在聊天之前做的,一起写了,也不分先后了 看一下效果图,上面是模块主导航,左侧是模块内菜单,现在加一下隐藏 ...

  3. Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token

    原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf?   因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...

  4. Prism for WPF 搭建一个简单的模块化开发框架(五)添加聊天、消息模块

    原文:Prism for WPF 搭建一个简单的模块化开发框架(五)添加聊天.消息模块 中秋节假期没事继续搞了搞 做了各聊天的模块,需要继续优化 第一步画页面 页面参考https://github.c ...

  5. Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单

    原文:Prism for WPF 搭建一个简单的模块化开发框架(三) 给TreeView加样式做成菜单 昨天晚上把TreeView的样式做了一下,今天给TreeView绑了数据,实现了切换页面功能 上 ...

  6. Prism for WPF 搭建一个简单的模块化开发框架(二)

    原文:Prism for WPF 搭建一个简单的模块化开发框架(二) 今天又有时间了,再改改,加了一些控件全局的样式 样式代码 <ResourceDictionary xmlns="h ...

  7. Prism for WPF 搭建一个简单的模块化开发框架(一)

    原文:Prism for WPF 搭建一个简单的模块化开发框架(一) 最近闲来无事又想搞搞WPF..... 做个框架吧,可能又是半途而废....总是坚持不下来 不废话了, 先看一下工程结构 布局大概是 ...

  8. Prism for WPF再探(基于Prism事件的模块间通信)

    上篇博文链接 Prism for WPF初探(构建简单的模块化开发框架) 一.简单介绍: 在上一篇博文中初步搭建了Prism框架的各个模块,但那只是搭建了一个空壳,里面的内容基本是空的,在这一篇我将实 ...

  9. Prism for WPF

    Prism for WPF Prism for WPF初探(构建简单的模块化开发框架)   先简单的介绍一下Prism框架,引用微软官方的解释: Prism provides guidance to ...

随机推荐

  1. css是如何实现在页面文字不换行、自动换行、强制换行的

    强制不换行 div{ white-space:nowrap; } 自动换行 div{ word-wrap: break-word; word-break: normal; } 强制英文单词断行 div ...

  2. 【唯星宠物】——CSS/BootStrap/Jquery爬坑之响应式首页

    前言:唯星宠物产品官网,分为首页.子页和登录注册页三个页面,除网页内容设计与图片素材的部分使用网上的材料之外,其余内容呈现以及功能模块全部为自己重构. 一.响应式轮播banner 思路:使用BootS ...

  3. VS2012 C#生成DLL并调用

    1.创建一个C#工程生成DLL 新建->项目->Visual C#->类库->MyMethods 项目建好后,为了理解,将项目中的Class1.cs 文件 重命名为 MySwa ...

  4. Marriage is Stable

    Marriage is Stable Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU 4325 Flowers(树状数组)

    Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  6. 附录三 关于book.h

    本书中用到的公用函数放到了头文件book.h中. #ifndef __BOOK_H__ #define __BOOK_H__ #include <stdio.h> #include < ...

  7. 使用javascript编写根据用户鼠标控制背景图片的移动

    在一家VR公司做前端. 起初进入前端就是一种内心的直觉,创造更好的用户体验,让页面更加友好,当然最起初接手web项目还是为了完成毕业设计. 一个网上图书商城,虽然不大,但五脏都有毕竟开刀所以避免不了很 ...

  8. C# linq创建嵌套组

    以下示例演示如何在 LINQ 查询表达式中创建嵌套组. 首先根据学生年级创建每个组,然后根据每个人的姓名进一步细分为小组. public void QueryNestedGroups() { var ...

  9. HTML5新特性之WebRTC[转]

    原文:http://www.cnblogs.com/jscode/p/3601648.html?comefrom=http://blogread.cn/news/ 1.概述 WebRTC是“网络实时通 ...

  10. eclipse安装checkstyle无法加载到preferences的问题

    描述一下问题,eclipse安装checkstyle,不管是在线安装还是下载安装,在preferences都没有checkstyle选项,如下: 然我们要的效果是这样的:   解决方案如下: 1 启动 ...