在實(shí)際應(yīng)用開發(fā)中,隨著項(xiàng)目業(yè)務(wù)逐漸復(fù)雜,耦合度會越來越高,維護(hù)成本也會直線上升,所以解耦也變得越來越重要。Prism框架為WPF開發(fā)中解耦提供了非常便捷的應(yīng)用。今天主要以一個簡單的小例子,簡述WPF開發(fā)中Prism框架的簡單應(yīng)用,如有不足之處,還請指正。
什么是Prism?
Prism是一個開源框架,用于在WPF、Xamarin Forms、Uno/Win UI等應(yīng)用中創(chuàng)建松耦合、可維護(hù)、可測試的XAML應(yīng)用程序。Prism提供了一組設(shè)計模式的實(shí)現(xiàn),這些設(shè)計模式有助于編寫結(jié)構(gòu)良好且可維護(hù)的XAML應(yīng)用程序,包括MVVM,dependency injection,commands,EventAggregator等。
Prism源碼庫
Prism遵守開源許可協(xié)議(MIT),目前最新版本8.1.97,可通過GitHub進(jìn)行下載最新版本。https://github.com/PrismLibrary
Prism優(yōu)點(diǎn)
?Prism 設(shè)計圍繞核心建筑設(shè)計原則,即關(guān)注點(diǎn)分離和松散耦合。這使得Prism可以提供許多好處
-
重復(fù)使用:通過重復(fù)使用單元測試的組件,可以通過依賴性注入在運(yùn)行時間輕松發(fā)現(xiàn)和集成,以及通過使用可在應(yīng)用程序中重復(fù)使用的應(yīng)用程序級功能封裝模塊,在應(yīng)用級別實(shí)現(xiàn)重復(fù)使用。
-
可擴(kuò)展性:通過管理組件依賴性、使組件在運(yùn)行時間更容易集成或替換為替代實(shí)現(xiàn)以及提供將應(yīng)用程序分解為可獨(dú)立更新和部署的模塊的能力,幫助創(chuàng)建易于擴(kuò)展的應(yīng)用程序
-
靈活性:Prism?有助于創(chuàng)建靈活的應(yīng)用程序,使它們能夠隨著新功能的開發(fā)和集成而更容易更新
-
團(tuán)隊(duì)發(fā)展:Prism?有助于最大限度地減少跨團(tuán)隊(duì)依賴性,并允許團(tuán)隊(duì)專注于不同的功能領(lǐng)域(如?UI?設(shè)計、業(yè)務(wù)邏輯實(shí)現(xiàn)和基礎(chǔ)架構(gòu)代碼開發(fā)),或不同業(yè)務(wù)級別的功能領(lǐng)域(如簡介、銷售、庫存或物流)。
?文章來源地址http://www.zghlxwxcb.cn/news/detail-414881.html
模塊化思想
通過對比發(fā)現(xiàn),采用模塊化思想進(jìn)行設(shè)計,使得程序結(jié)構(gòu)清晰,符合高內(nèi)聚,低耦合的設(shè)計風(fēng)格。
![]() |
![]() |
?
?
Prism安裝
Prism可通過NuGet方案包管理器進(jìn)行安裝,主要安裝三個Prism.Core,Prism.Unity,Prism.Wpf
?
創(chuàng)建模塊和視圖控件
創(chuàng)建WPF類庫,并添加用戶控件視圖,并采用MVVM開發(fā)模式
![]() |
|
?
數(shù)據(jù)綁定
在Prism框架中,提供了數(shù)據(jù)綁定基類Prism.Mvvm.BindableBase,可以方便的將普通屬性,轉(zhuǎn)換為依賴屬性,簡化開發(fā)中過程中的代碼量。
namespace DemoPrism.Second.ViewModels
{
internal class SecondViewModel : BindableBase
{
#region 屬性及構(gòu)造函數(shù)
private int id;
public int Id
{
get { return id; }
set { SetProperty(ref id, value); }
}
/// <summary>
/// 模塊間交互
/// </summary>
private readonly IEventAggregator eventAggregator;
public SecondViewModel(IEventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
this.eventAggregator.GetEvent<DemoOneEvent>().Subscribe(DemoOneRecived);
}
#endregion
private void DemoOneRecived(int id)
{
this.Id = id;
}
}
}
創(chuàng)建Prism模塊
添加Module類,并實(shí)現(xiàn)Prism.Modularity.IModule接口,實(shí)現(xiàn)接口的模塊,視為可以被Prism發(fā)現(xiàn)并加載的模塊。以DefectListModule模塊為例:
namespace DemoPrism.First
{
public class FirstModule : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
IRegionManager regionManager = containerProvider.Resolve<IRegionManager>();
regionManager.RegisterViewWithRegion("FirstRegion", typeof(Views.FirstView));
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<Views.FirstView, ViewModels.FirstViewModel>();
}
}
}
模塊配置
Prism提供了多種模塊加載方式,常用的有App.config配置文件方法。
-
在App.config節(jié)點(diǎn),添加configSections配置,增加modules節(jié)點(diǎn)配置
-
modules節(jié)點(diǎn)主要配置需要加載的Prism模塊
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!--prism配置-->
<section name="modules" type="Prism.Modularity.ModulesConfigurationSection, Prism.Wpf"/>
</configSections>
<modules>
<!--注冊模塊-->
<module assemblyFile="Modules\DemoPrism.First.dll" moduleType="DemoPrism.First.FirstModule, DemoPrism.First" moduleName="First" startupLoaded="true" />
<module assemblyFile="Modules\DemoPrism.Second.dll" moduleType="DemoPrism.Second.SecondModule, DemoPrism.Second" moduleName="Second" startupLoaded="true" />
</modules>
</configuration>
模塊加載
模塊配置好后,需要在啟動的時候,加載模塊。修改WPF入口啟動程序,App.xaml.cs文件,繼承自Prism.Unity.PrismApplication基類,并重寫相關(guān)初始化
?
namespace DemoPrism
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
//使用容器創(chuàng)建主窗體
protected override Window CreateShell() => Container.Resolve<MainWindow>();
protected override void ConfigureViewModelLocator()
{
base.ConfigureViewModelLocator();
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
//通過代碼的方式添加模塊
//moduleCatalog.AddModule<NavigationModule.NavigationModule>();
//將MedicineModule模塊設(shè)置為按需加載
base.ConfigureModuleCatalog(moduleCatalog);
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
protected override IModuleCatalog CreateModuleCatalog()
{
ConfigurationModuleCatalog configurationModuleCatalog = new ConfigurationModuleCatalog();
configurationModuleCatalog.Load();
//通過Xaml配置文件讀取模塊加載信息
return configurationModuleCatalog;
//return directoryModuleCatalog;
}
/// <summary>
/// 注冊適配器(區(qū)域容器:Region)
/// </summary>
/// <param name="regionAdapterMappings"></param>
protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
{
base.ConfigureRegionAdapterMappings(regionAdapterMappings);
}
}
}
?
區(qū)域Region
在Prism框架中,模塊可以注冊到導(dǎo)航菜單Navigation,也可以注冊到區(qū)域Region,根據(jù)實(shí)際業(yè)務(wù)需要進(jìn)行選擇。Region可以更加方便的進(jìn)行模塊化布局等。在普通容器控件中,增加prism:RegionManager.RegionName=”名稱”
<Window x:Class="DemoPrism.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
xmlns:local="clr-namespace:DemoPrism"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
prism:ViewModelLocator.AutoWireViewModel="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<ContentControl Grid.Column="0" prism:RegionManager.RegionName="FirstRegion"></ContentControl>
<ContentControl Grid.Column="1" prism:RegionManager.RegionName="SecondRegion"></ContentControl>
</Grid>
</Window>
模塊交互
模塊與模塊之間相互獨(dú)立,如果需要交互,可以通過事件聚合器IEventAggregator,采用事件的訂閱和發(fā)布進(jìn)行通信。
事件訂閱步驟:
-
定義事件,定義一個類,繼承自Prism.Events.PubSubEvent泛型類
-
事件發(fā)布,通過事件聚合器的Publish方法進(jìn)行發(fā)布。
-
事件訂閱,通過事件聚合器的Subscribe進(jìn)行訂閱。
namespace DemoPrism.Event
{
/// <summary>
/// 注冊事件
/// </summary>
public class DemoOneEvent : PubSubEvent<int>
{
}
}
彈出模態(tài)窗口
?在Prism框架下,彈出模態(tài)窗口,需要以下3個步驟:
-
在Prism框架中,頁面UserControl實(shí)現(xiàn)彈窗功能,被彈出頁面需要實(shí)現(xiàn)Prism.Services.Dialogs.IDialogAware接口。
-
注冊窗口,將UserControl注冊成窗口。
-
調(diào)用彈出服務(wù),彈出窗口
源碼下載
示例中源碼下載,在公眾號恢復(fù)關(guān)鍵詞PRISM,如下所示:
學(xué)習(xí)編程,從關(guān)注【老碼識途】開始?。?!文章來源:http://www.zghlxwxcb.cn/news/detail-414881.html
?
到了這里,關(guān)于WPF開發(fā)之Prism詳解【內(nèi)附源碼】的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!