1.概要

源码及PPT地址:https://github.com/JusterZhu/wemail

视频地址:https://www.bilibili.com/video/BV1KQ4y1C7tg?sharesource=copyweb

(1)Prism概览

  • Application:我们开发应用程序,初始化Bootstrapper。

  • Bootstrapper:是用来初始化应用程序级别的组件和服务,它也被用来配置和初始化module catalog和Shell 的View和View Model。

  • Modules:是能够独立开发、测试、部署的功能单元,Modules可以被设计成实现特定业务逻辑的模块(如Profile Management),也可以被设计成实现通用基础设施或服务的模块。

  • Shell是宿主应用程序(host application),modules将会被load到Shell中。Shell定义了应用程序的整体布局和结构,而不关心寄宿其中的Module,Shell通常实现通用的application service和infrastructure,而应用的逻辑则实现在具体的Module中,同时,Shell也提供了应用程序的顶层窗口。

  • Services:是用来实现非UI相关功能的逻辑,例如logging、exception management、data access。Services可以被定义在应用程序中或者是Module中,Services通常被注册在依赖注入容器中,使得其它的组件可以很容易的定位这个服务。

  • Container:注入服务、其他模块依赖。

(2)Region

  • Region是应用程序UI的逻辑区域(具体的表现为容器控件),Views在Region中展现,很多种控件可以被用作Region:ContentControl、ItemsControl、ListBox、TabControl。Views能在Regions编程或者自动呈现,Prism也提供了Region导航的支持。这么设计主要为了解耦让内容显示灵活具有多样性。

在实战项目当中,需根据业务需求来划分Region。

(3)RegionManager

RegionManager主要实现维护区域集合、提供对区域的访问、合成视图、区域导航、定义区域。

区域定义方式有两种:

  • Xaml实现

    <ContentControl x:Name=“ContentCtrl” prism:RegionManager.RegionName="ContentRegion" />
  • C#实现

    RegionManager.SetRegionName(ContentCtrl,”ContentRegion”);public MainWindowViewModel(IRegionManager regionManager)
    {_regionManager = regionManager;_regionManager.RegisterViewWithRegion("ContentRegion", typeof(MainWindow));
    }

(4)RegionAdapter

RegionAdapter(区域适配)主要作用为特定的控件创建相应的Region,并将控件与Region进行绑定,然后为Region添加一些行为。

因为并不是所有的控件都可以作为Region的,需要为需要定义为Region的控件添加RegionAdapter。一个RegionAdapter需要实现IRegionAdapter接口,如果你需要自定义一个RegionAdapter,可以通过继承RegionAdapterBase类来省去一些工作。

Prism为开发者提供了几个默认RegionAdapter:

  • ContentControlRegionAdapter:创建一个SingleActiveRegion并将其与ContentControl绑定

  • ItemsControlRegionAdapter:创建一个AllActiveRegion并将其与ItemsControl绑定

  • SelectorRegionAdapter:创建一个Region并将其与Selector绑定

  • TabControlRegionAdapter:创建一个Region并将其与TabControl绑定

2.详细内容

Region和RegionManager实战应用。

(1)定义Region及选择好容器控件

<TabControl prism:RegionManager.RegionName="TabRegion" />

(2)ViewModel注册视图到TabRegion当中 public class MainWindowViewModel : BindableBase { //Region管理对象 private IRegionManager _regionManager; private string _title = "Prism Application";

public string Title{get { return _title; }set { SetProperty(ref _title, value); }}public MainWindowViewModel(IRegionManager regionManager){//Prism框架内依赖注入的RegionManager_regionManager = regionManager;//在ContentRegion中注册视图TempView(TabItem1)_regionManager.RegisterViewWithRegion("TabRegion", typeof(TempView));//TabItem2_regionManager.RegisterViewWithRegion("TabRegion", typeof(Temp2View));//TabItem3_regionManager.RegisterViewWithRegion("WorkRegion", typeof(Temp3View));//对视图的访问、操作//var contentRegion = _regionManager.Regions["ContentRegion"];//contentRegion.Context//contentRegion.Remove()//contentRegion.Activate()//foreach (var item in contentRegion.ActiveViews)//{//    contentRegion.Activate(item);//}}
}

RegionAdapter实战应用。

如果在实际开发工作当中遇到了特殊场景需要而Prism并没有设置对应的RegionAdapter。这时候可以通过继承实现RegionAdapterBase内置对象来扩展一个新的RegionAdapter。

(1)实现一个新的RegionAdapter
/// <summary>
/// custom region adapter.
/// </summary>
public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
{public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory){}protected override void Adapt(IRegion region, StackPanel regionTarget){//该事件监听往StackPanel添加view时的操作region.Views.CollectionChanged += (sender, e) =>{//监听到增加操作时则往StackPanel添加Children,枚举出来的操作在后面一段代码中体现if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add){regionTarget.Children.Clear();foreach (var item in e.NewItems){regionTarget.Children.Add(item as UIElement);}}};}protected override IRegion CreateRegion(){return new Region();}
}// Summary:
//     Describes the action that caused a System.Collections.Specialized.INotifyCollectionChanged.CollectionChanged
//     event.
public enum NotifyCollectionChangedAction
{//// Summary://     An item was added to the collection.Add,//// Summary://     An item was removed from the collection.Remove,//// Summary://     An item was replaced in the collection.Replace,//// Summary://     An item was moved within the collection.Move,//// Summary://     The contents of the collection changed dramatically.Reset
}

(2)在App.cs文件中注册新的RegionAdapter

public partial class App
{/// <summary>/// 应用程序启动时创建Shell/// </summary>/// <returns></returns>protected override Window CreateShell(){return Container.Resolve<MainWindow>();}protected override void RegisterTypes(IContainerRegistry containerRegistry){}/// <summary>/// 配置区域适配/// </summary>/// <param name="regionAdapterMappings"></param>protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings){base.ConfigureRegionAdapterMappings(regionAdapterMappings);//添加自定义区域适配对象,会自动适配标记上prism:RegionManager.RegionName的容器控件为RegionregionAdapterMappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());}
}

(3)在xaml中使用

<StackPanel prism:RegionManager.RegionName="StackPanelRegion"></StackPanel>

03Prism WPF 入门实战 - Region相关推荐

  1. 04Prism WPF 入门实战 - Module

    1.概要 Module,具有特定功能,且独立存在则称为成为模块.下图为Prism体系中的关系结构图. 在Prism体系中Module的应用分为 注册/发现模块 加载模块 初始化模块 2.详细内容 (1 ...

  2. 01Prism WPF 入门实战 - 项目准备

    1.概要 这一系列将进行Prism+WPF技术的实战讲解.实战项目内容选型为Email邮件收发的客户端(WeMail),项目结构简单方便大家理解. 相关技术:C#.WPF.Prism 软件开发环境:V ...

  3. 06Prism WPF 入门实战 - Log控件库

    1.概要 源码及PPT地址:https://github.com/JusterZhu/wemail 视频地址:https://www.bilibili.com/video/BV1KQ4y1C7tg?s ...

  4. 05Prism WPF 入门实战 - Navigation

    1.概要 源码及PPT地址:https://github.com/JusterZhu/wemail 视频地址:https://www.bilibili.com/video/BV1KQ4y1C7tg?s ...

  5. 02Prism WPF 入门实战 - 建项

    1.概要 Prism介绍 Github:  https://github.com/PrismLibrary/Prism 开发文档:https://prismlibrary.com/docs/ Pris ...

  6. WPF入门教程-转载

    最近为了做炫酷的UI,了解了WPF,之前一直是使用winform的,界面也是古老的不行. 在园里找到了一个大佬以前写的教程,备注一下.按照系列教程走下来,可以直接上手了. 备忘传送门>>& ...

  7. WPF入门(三)-几何图形之不规则图形(PathGeometry) (2)

    WPF入门(三)->几何图形之不规则图形(PathGeometry) (2) 原文:WPF入门(三)->几何图形之不规则图形(PathGeometry) (2) 上一节我们介绍了PathG ...

  8. Fastlane 入门实战教程从打包到上传iTunes connect

    有关神器 Fastlane 持续集成\部署的文章网上挺多,本文定位是入门教程,针对 iOS 应用的持续部署,只需一条命令就可实现从 Xcode 项目到 编译\打包\构建\提交审核 文章稍微有点长,涵盖 ...

  9. WPF入门:数据绑定

    原文:WPF入门:数据绑定 上一篇我们将XAML大概做了个了解 ,这篇将继续学习WPF数据绑定的相关内容 数据源与控件的Binding Binding作为数据传送UI的通道,通过INotityProp ...

最新文章

  1. Dispatch 执行ABC任务,执行完成之后刷新UI,指定任务D
  2. sklearn svm如何选择核函数_使用python+sklearn实现成对度量、相关性和核函数
  3. CodeForces 258D Little Elephant and Broken Sorting(期望)
  4. 一些基于Java的AI框架:Encog,JavaML,Weka
  5. spring jdbctemplate 实体列与数据表列 查询
  6. 【ZOJ - 4032】Magic Points (思维,几何,构造)
  7. npm ERR! Please try running this command again as root/Administrator.
  8. 章程系统管理明天软考的童鞋进来顶起!!!
  9. 获取pycharm 2016.1.4 注册码 (window系统) 2016.3注册码(Ubuntu系统)
  10. 最小二乘法、梯度下降法和两者区别
  11. 银行客户交易行为预测:LightGBM模型
  12. UltraEdit 25注册机 通用版 32/64位 绿色免费版(附破解激活教程+序列号)
  13. 《圆明园的毁灭》教学设计方案
  14. OpenPose Demo
  15. webmax函数高级教程整理集
  16. 网络聊天室项目说明书
  17. djs-050微型计算机,合肥造DJS-050机—中国微机的摇篮
  18. JS写下雨特效,樱花落特效,滑块成功效果
  19. win10计算机 需要新应用,解决win10应用商店提示“需要新应用打开此ms-windows-store”的步骤...
  20. 关于戴尔电脑物理内存(灵越7590)

热门文章

  1. SQL相关路径查询脚本
  2. 小凡模拟器:DynamipsGUI使用问题解决方法
  3. Arts 第十九周(7/22 ~ 7/28)
  4. 2018-2019-1 20165234 《信息安全系统设计基础》第四周学习总结
  5. CS Academy Gcd Rebuild
  6. spark-2.1.0 集群安装
  7. ReactNative--React简介
  8. 补作业:随机生成二元四则运算
  9. Ctrl与Caps Lock键的交换
  10. UNIX环境高级编程笔记