03Prism WPF 入门实战 - Region
1.概要
源碼及PPT地址:https://github.com/JusterZhu/wemail
視頻地址:https://www.bilibili.com/video/BV1KQ4y1C7tg?sharesource=copyweb
(1)Prism概覽
Application:我們開發(fā)應用程序,初始化Bootstrapper。
Bootstrapper:是用來初始化應用程序級別的組件和服務,它也被用來配置和初始化module catalog和Shell 的View和View Model。
Modules:是能夠獨立開發(fā)、測試、部署的功能單元,Modules可以被設計成實現(xiàn)特定業(yè)務邏輯的模塊(如Profile Management),也可以被設計成實現(xiàn)通用基礎設施或服務的模塊。
Shell是宿主應用程序(host application),modules將會被load到Shell中。Shell定義了應用程序的整體布局和結構,而不關心寄宿其中的Module,Shell通常實現(xiàn)通用的application service和infrastructure,而應用的邏輯則實現(xiàn)在具體的Module中,同時,Shell也提供了應用程序的頂層窗口。
Services:是用來實現(xiàn)非UI相關功能的邏輯,例如logging、exception management、data access。Services可以被定義在應用程序中或者是Module中,Services通常被注冊在依賴注入容器中,使得其它的組件可以很容易的定位這個服務。
Container:注入服務、其他模塊依賴。
(2)Region
Region是應用程序UI的邏輯區(qū)域(具體的表現(xiàn)為容器控件),Views在Region中展現(xiàn),很多種控件可以被用作Region:ContentControl、ItemsControl、ListBox、TabControl。Views能在Regions編程或者自動呈現(xiàn),Prism也提供了Region導航的支持。這么設計主要為了解耦讓內(nèi)容顯示靈活具有多樣性。
在實戰(zhàn)項目當中,需根據(jù)業(yè)務需求來劃分Region。
(3)RegionManager
RegionManager主要實現(xiàn)維護區(qū)域集合、提供對區(qū)域的訪問、合成視圖、區(qū)域導航、定義區(qū)域。
區(qū)域定義方式有兩種:
Xaml實現(xiàn)
<ContentControl x:Name=“ContentCtrl” prism:RegionManager.RegionName="ContentRegion" />C#實現(xiàn)
RegionManager.SetRegionName(ContentCtrl,”ContentRegion”);public MainWindowViewModel(IRegionManager regionManager) {_regionManager = regionManager;_regionManager.RegisterViewWithRegion("ContentRegion", typeof(MainWindow)); }
(4)RegionAdapter
RegionAdapter(區(qū)域適配)主要作用為特定的控件創(chuàng)建相應的Region,并將控件與Region進行綁定,然后為Region添加一些行為。
因為并不是所有的控件都可以作為Region的,需要為需要定義為Region的控件添加RegionAdapter。一個RegionAdapter需要實現(xiàn)IRegionAdapter接口,如果你需要自定義一個RegionAdapter,可以通過繼承RegionAdapterBase類來省去一些工作。
Prism為開發(fā)者提供了幾個默認RegionAdapter:
ContentControlRegionAdapter:創(chuàng)建一個SingleActiveRegion并將其與ContentControl綁定
ItemsControlRegionAdapter:創(chuàng)建一個AllActiveRegion并將其與ItemsControl綁定
SelectorRegionAdapter:創(chuàng)建一個Region并將其與Selector綁定
TabControlRegionAdapter:創(chuàng)建一個Region并將其與TabControl綁定
2.詳細內(nèi)容
Region和RegionManager實戰(zhàn)應用。
(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框架內(nèi)依賴注入的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實戰(zhàn)應用。
如果在實際開發(fā)工作當中遇到了特殊場景需要而Prism并沒有設置對應的RegionAdapter。這時候可以通過繼承實現(xiàn)RegionAdapterBase內(nèi)置對象來擴展一個新的RegionAdapter。
(1)實現(xiàn)一個新的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){//該事件監(jiān)聽往StackPanel添加view時的操作region.Views.CollectionChanged += (sender, e) =>{//監(jiān)聽到增加操作時則往StackPanel添加Children,枚舉出來的操作在后面一段代碼中體現(xiàn)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>/// 應用程序啟動時創(chuàng)建Shell/// </summary>/// <returns></returns>protected override Window CreateShell(){return Container.Resolve<MainWindow>();}protected override void RegisterTypes(IContainerRegistry containerRegistry){}/// <summary>/// 配置區(qū)域適配/// </summary>/// <param name="regionAdapterMappings"></param>protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings){base.ConfigureRegionAdapterMappings(regionAdapterMappings);//添加自定義區(qū)域適配對象,會自動適配標記上prism:RegionManager.RegionName的容器控件為RegionregionAdapterMappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());} }(3)在xaml中使用
<StackPanel prism:RegionManager.RegionName="StackPanelRegion"></StackPanel>總結
以上是生活随笔為你收集整理的03Prism WPF 入门实战 - Region的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dapr牵手.NET学习笔记:Actor
- 下一篇: 为什么应该在业务层实现管道模式,而不用A