日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

03Prism WPF 入门实战 - Region

發(fā)布時(shí)間:2023/12/4 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 03Prism WPF 入门实战 - Region 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.概要

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

視頻地址:https://www.bilibili.com/video/BV1KQ4y1C7tg?sharesource=copyweb

(1)Prism概覽

  • Application:我們開發(fā)應(yīng)用程序,初始化Bootstrapper。

  • Bootstrapper:是用來初始化應(yīng)用程序級(jí)別的組件和服務(wù),它也被用來配置和初始化module catalog和Shell 的View和View Model。

  • Modules:是能夠獨(dú)立開發(fā)、測試、部署的功能單元,Modules可以被設(shè)計(jì)成實(shí)現(xiàn)特定業(yè)務(wù)邏輯的模塊(如Profile Management),也可以被設(shè)計(jì)成實(shí)現(xiàn)通用基礎(chǔ)設(shè)施或服務(wù)的模塊。

  • Shell是宿主應(yīng)用程序(host application),modules將會(huì)被load到Shell中。Shell定義了應(yīng)用程序的整體布局和結(jié)構(gòu),而不關(guān)心寄宿其中的Module,Shell通常實(shí)現(xiàn)通用的application service和infrastructure,而應(yīng)用的邏輯則實(shí)現(xiàn)在具體的Module中,同時(shí),Shell也提供了應(yīng)用程序的頂層窗口。

  • Services:是用來實(shí)現(xiàn)非UI相關(guān)功能的邏輯,例如logging、exception management、data access。Services可以被定義在應(yīng)用程序中或者是Module中,Services通常被注冊在依賴注入容器中,使得其它的組件可以很容易的定位這個(gè)服務(wù)。

  • Container:注入服務(wù)、其他模塊依賴。

(2)Region

  • Region是應(yīng)用程序UI的邏輯區(qū)域(具體的表現(xiàn)為容器控件),Views在Region中展現(xiàn),很多種控件可以被用作Region:ContentControl、ItemsControl、ListBox、TabControl。Views能在Regions編程或者自動(dòng)呈現(xiàn),Prism也提供了Region導(dǎo)航的支持。這么設(shè)計(jì)主要為了解耦讓內(nèi)容顯示靈活具有多樣性。

在實(shí)戰(zhàn)項(xiàng)目當(dāng)中,需根據(jù)業(yè)務(wù)需求來劃分Region。

(3)RegionManager

RegionManager主要實(shí)現(xiàn)維護(hù)區(qū)域集合、提供對區(qū)域的訪問、合成視圖、區(qū)域?qū)Ш健⒍x區(qū)域。

區(qū)域定義方式有兩種:

  • Xaml實(shí)現(xiàn)

    <ContentControl x:Name=“ContentCtrl” prism:RegionManager.RegionName="ContentRegion" />
  • C#實(shí)現(xiàn)

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

(4)RegionAdapter

RegionAdapter(區(qū)域適配)主要作用為特定的控件創(chuàng)建相應(yīng)的Region,并將控件與Region進(jìn)行綁定,然后為Region添加一些行為。

因?yàn)椴⒉皇撬械目丶伎梢宰鳛镽egion的,需要為需要定義為Region的控件添加RegionAdapter。一個(gè)RegionAdapter需要實(shí)現(xiàn)IRegionAdapter接口,如果你需要自定義一個(gè)RegionAdapter,可以通過繼承RegionAdapterBase類來省去一些工作。

Prism為開發(fā)者提供了幾個(gè)默認(rèn)RegionAdapter:

  • ContentControlRegionAdapter:創(chuàng)建一個(gè)SingleActiveRegion并將其與ContentControl綁定

  • ItemsControlRegionAdapter:創(chuàng)建一個(gè)AllActiveRegion并將其與ItemsControl綁定

  • SelectorRegionAdapter:創(chuàng)建一個(gè)Region并將其與Selector綁定

  • TabControlRegionAdapter:創(chuàng)建一個(gè)Region并將其與TabControl綁定

2.詳細(xì)內(nèi)容

Region和RegionManager實(shí)戰(zhàn)應(yīng)用。

(1)定義Region及選擇好容器控件

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

(2)ViewModel注冊視圖到TabRegion當(dāng)中 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實(shí)戰(zhàn)應(yīng)用。

如果在實(shí)際開發(fā)工作當(dāng)中遇到了特殊場景需要而Prism并沒有設(shè)置對應(yīng)的RegionAdapter。這時(shí)候可以通過繼承實(shí)現(xiàn)RegionAdapterBase內(nèi)置對象來擴(kuò)展一個(gè)新的RegionAdapter。

(1)實(shí)現(xiàn)一個(gè)新的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時(shí)的操作region.Views.CollectionChanged += (sender, e) =>{//監(jiān)聽到增加操作時(shí)則往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>/// 應(yīng)用程序啟動(dòng)時(shí)創(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ū)域適配對象,會(huì)自動(dòng)適配標(biāo)記上prism:RegionManager.RegionName的容器控件為RegionregionAdapterMappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());} }

(3)在xaml中使用

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

總結(jié)

以上是生活随笔為你收集整理的03Prism WPF 入门实战 - Region的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。