03Prism WPF 入門實戰 - Region

1。概要

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

影片地址:https://www.bilibili.com/video/BV1KQ4y1C7tg?sharesource=copyweb

(1)Prism概覽

03Prism WPF 入門實戰 - Region

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:注入服務、其他模組依賴。

03Prism WPF 入門實戰 - Region

03Prism WPF 入門實戰 - Region

(2)Region

Region是應用程式UI的邏輯區域(具體的表現為容器控制元件),Views在Region中展現,很多種控制元件可以被用作Region:ContentControl、ItemsControl、ListBox、TabControl。Views能在Regions程式設計或者自動呈現,Prism也提供了Region導航的支援。這麼設計主要為了解耦讓內容顯示靈活具有多樣性。

03Prism WPF 入門實戰 - Region

在實戰專案當中,需根據業務需求來劃分Region。

03Prism WPF 入門實戰 - 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實戰應用。

03Prism WPF 入門實戰 - Region

(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);

//}

}

}

03Prism WPF 入門實戰 - Region

RegionAdapter實戰應用。

如果在實際開發工作當中遇到了特殊場景需要而Prism並沒有設定對應的RegionAdapter。這時候可以透過繼承實現RegionAdapterBase內建物件來擴充套件一個新的RegionAdapter。

1

)實現一個新的RegionAdapter

///

/// custom region adapter。

///

public

class

StackPanelRegionAdapter : RegionAdapterBase

{

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

{

///

///

應用程式啟動時建立Shell

///

///

protected

override

Window

CreateShell

{

return

Container。Resolve();

}

protected

override

void

RegisterTypes

IContainerRegistry containerRegistry

{

}

///

///

配置區域適配

///

///

protected

override

void

ConfigureRegionAdapterMappings

RegionAdapterMappings regionAdapterMappings

{

base

。ConfigureRegionAdapterMappings(regionAdapterMappings);

//新增自定義區域適配物件,會自動適配標記上prism:RegionManager。RegionName的容器控制元件為Region

regionAdapterMappings。RegisterMapping(

typeof

(StackPanel), Container。Resolve());

}

}

(3)在xaml中使用

<

StackPanel

prism:RegionManager。RegionName

=

“StackPanelRegion”

>

StackPanel

>