C# WPF MVVM模式Prism框架从零搭建(经典)
01
—
前言
目前最新的PRISM的版本是8.1.97,本節以6.3.0.0 講解,可以在Github上獲取PRISM的源碼。
Prism Github地址:https://github.com/PrismLibrary/Prism
Prism官方文檔:https://prismlibrary.com/docs/
Prism要用到IOC容器,提供選擇的有Unity和MEF,這里我分別采用MEF和unity去做,不懂MEF的建議看看這位大牛的系列博文http://www.cnblogs.com/yunfeifei/p/3922668.html
02
—
安裝庫
在nuget上安裝Prism相關常用的庫
03
—
項目搭建
step1:新建解決方案:我這里命名為PrismFrameTest;
step2:刪除MainWindow.xaml,刪除App.xaml中啟動引導
StartupUri="MainWindow.xaml"然后在App.xaml.cs新建程序入口
protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);MyBootstrapper bootStrapper = new MyBootstrapper();bootStrapper.Run(true);}新建引導類MyBootstrapper.cs,需要繼承基類Prism.Mef庫下的基類MefBootstrapper
方式1 采用mef
public class MyBootstrapper : MefBootstrapper{protected override DependencyObject CreateShell(){return this.Container.GetExportedValue<MyShellView>();}protected override void InitializeShell(){base.InitializeShell();Application.Current.MainWindow = (MyShellView)this.Shell;Application.Current.MainWindow.Show();//Show主窗口,但content內沒有內容,只有當調用Module中的Initialize()方法后才將HelloWorldView顯示出來。}protected override void ConfigureAggregateCatalog(){base.ConfigureAggregateCatalog();this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(MyBootstrapper).Assembly));this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(PrismModuleLeft.ModuleLeftViewModel).Assembly));//注冊模塊//this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleB.ModuleBViewModel).Assembly));}protected override IModuleCatalog CreateModuleCatalog(){// When using MEF, the existing Prism ModuleCatalog is still the place to configure modules via configuration files.return new ConfigurationModuleCatalog();}}方式2?采用unity
public class MyBootstrapper : UnityBootstrapper{protected override DependencyObject CreateShell(){return Container.Resolve<MyShellView>();}protected override void InitializeShell(){base.InitializeShell();Application.Current.MainWindow = (MyShellView)this.Shell;Application.Current.MainWindow.Show();//Show主窗口,但content內沒有內容,只有當調用Module中的Initialize()方法后才將HelloWorldView顯示出來。}protected override void ConfigureModuleCatalog(){base.ConfigureModuleCatalog();ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;moduleCatalog.AddModule(typeof(PrismModuleLeft.ModuleLeftViewModel));//注冊模塊}}step3:
然后新建一個xaml窗體MyShellView.xaml,將窗體分為左右兩部分
這里cal:RegionManager.RegionName是一個依賴屬性,我們將它與ItemsControl控件相關聯,MainRegion就是一個占位符。
<ItemsControl cal:RegionManager.RegionName="RegionLeft" HorizontalAlignment="Center" VerticalAlignment="Center"/><ItemsControl?cal:RegionManager.RegionName="RegionRight"?HorizontalAlignment="Center"?VerticalAlignment="Center"?Grid.Column="1"/>對應的cs中將類標注為? [Export]
step4:新建類庫PrismModuleLeft
類庫中新建ModuleLeftView.xaml
關于事件綁定:(在下面代碼中兩種方式都列出來了)
①控件繼承自ButtonBase、MenuItem類,比如:Button、RadioButton、Hyperlink、MenuItem……這種情況下,由于Prism已經幫我們實現了這些控件的Command屬性,可以直接綁定Command屬性來完成Click事件到ViewModel的綁定:
②ListView、ListBox、DropDownList等等大部分沒有Click事件的控件。這時候,當我們要實現SelectedItemChanged、SelectionChanged等常用事件的時候,使用Expression Blend附帶的System.Windows.Interactivity.dll文件,它使用interaction trigger和InvokeCommandAction behavior來幫助我們直接綁定控件的事件。
需要引用
對應的cs中:
[Export]public partial class ModuleLeftView : UserControl{private readonly IRegionViewRegistry regionViewRegistry;public ModuleLeftView(){InitializeComponent();this.DataContext = new ModuleLeftViewModel(regionViewRegistry);}}step4:ModuleLeftViewModel中:
using Prism.Commands; using Prism.Mef.Modularity; using Prism.Modularity; using Prism.Mvvm; using Prism.Regions; using PropertyChanged; using System.ComponentModel.Composition; using System.Windows; using System.Windows.Input;namespace PrismModuleLeft {[AddINotifyPropertyChangedInterface][ModuleExport("ModuleLeftViewModel", typeof(ModuleLeftViewModel), InitializationMode = InitializationMode.WhenAvailable)]public class ModuleLeftViewModel : BindableBase,IModule{private readonly IRegionViewRegistry regionViewRegistry;public ICommand CreateRecipeCommand { get; set; }public DelegateCommand<KeyEventArgs> KeyUpEventCommand { get; private set; }public string TxtLabel { get; set; } = "Hello! I am ModuleA";public void KeyUpEventHandler(KeyEventArgs args){MessageBox.Show("PrismCTR");}public void Initialize(){regionViewRegistry.RegisterViewWithRegion("RegionLeft", typeof(ModuleLeftView));}[ImportingConstructor]public ModuleLeftViewModel(IRegionViewRegistry registry){this.regionViewRegistry = registry;CreateRecipeCommand = new DelegateCommand(() => CreateRecipe()); }public void CreateRecipe(){TxtLabel = "this is my first prism test example";MessageBox.Show(TxtLabel);}} }04
—
總結
這個時候我們來對PRISM的基礎架構做一個簡單的總結:
Shell: 主窗口,他的功能都是通過Module來實現的;
Bootstrapper: 應用程序的入口點;
Region: 內容區域,類似于一個占位符
Module: 真正實現業務功能的東西,是View,數據,模型組成的集合;
Prism是個非常強大的wpf mvvm模式框架,它使用依賴注入,控制反轉容器來幫助我們解決團隊合作的松耦合問題。
05
—
結果演示
05
—
源碼
鏈接:https://pan.baidu.com/s/1utVT-087R1WonjoHZrv_Iw
提取碼:在下面公眾號后臺,發送:提取碼,即可獲取
技術群:?需要進技術群的添加小編微信zls20210502 ,備注:加群;
06
—
經典回顧
? ? ? 因為公眾號平臺更改了推送規則,如果不想錯過內容,記得讀完點一下“贊”和“在看”,這樣每次新文章推送才會第一時間出現在你的訂閱列表里。點擊“贊”和“在看”支持我們吧!
往期推薦
C# WPF框架Caliburn.Micro入門實例1
C# WPF MVVM項目實戰(進階①)
C# WPF MVVM項目實戰(進階②)
C# WPF框架Caliburn.Micro快速搭建
C# WPF項目實戰
C# WPF mvvm模式下combobox綁定(list<enum>、Dictionary<int,string>)
C# WPF MVVM模式下在主窗體顯示子窗體并獲取結果
C# WPF Caliburn.Micro框架下利用Mef加載其它項目界面
C# WPF文本框TextEdit不以科學計數法顯示
C# 通過正則表達式來限制控件輸入有效性
C# datagridview、datagrid、GridControl增加行號
C# =>符號的使用
C# 無意間寫了一段線程死鎖的代碼
C# 看懂這100+行代碼,你就真正入門了(經典)
C# WPF項目實戰(經典)
WPF 如何修改button圓角(經典)
WPF XAML 為項目設置全局樣式
總結
以上是生活随笔為你收集整理的C# WPF MVVM模式Prism框架从零搭建(经典)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win11安卓极速教程!
- 下一篇: .NET 6新特性试用 | 隐式usin