WPF Prism框架介绍
WPF Prism (一) Region
WPF Prism(二)Module
WPF Prism(三)ViewModelLocator
WPF Prism(四)MVVM
WPF Prism(五)Navigation
WPF Prism(六)Dialog
一、什么是Prism
Prism是一個用于在 WPF、Xamarin Form、Uno 平臺和 WinUI 中構(gòu)建松散耦合、可維護(hù)和可測試的 XAML 應(yīng)用程序框架。
二、官方地址
https://github.com/PrismLibrary/Prism
基于Prism的VisualStudio擴展包模板: Prism Template Pack,下載它后可以快速創(chuàng)建基于Prism的項目。
三、Prism框架內(nèi)容
框架中包括 MVVM、依賴注入、Command、Message Event、導(dǎo)航、彈窗等功能。
四、Prism知識章節(jié)
- 1.Region(區(qū)域管理)
- 2.Module(模塊)
- 3.View Injection(視圖注入)
- 4.ViewModelLocationProvider(視圖模型定位)
- 5.Command(綁定相關(guān))
- 6.Eevent Aggregator (事件聚合器)
- 7.Navigation(導(dǎo)航)
- 8.Dialog(對話框)
五、創(chuàng)建一個Prism應(yīng)用程序
1、安裝VS拓展包:Prism Template Pack(已有則不需要安裝)。
2、添加新項目并選擇項目模板:Prism Blank App(.NET Core)。
3、項目創(chuàng)建過程中會讓你選擇一個容器,我們選擇了Unity容器,具體使用可以https://blog.csdn.net/zhudaokuan/article/details/111227077。
4、這樣一個空白的.NET Core Prism WPF項目就創(chuàng)建成功了。
那么我們來看看它到底有什么不一樣的地方。
5、首先查看App.xaml和App.xaml.cs文件
<prism:PrismApplicationx:Class="PrismBlankAppCore.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:PrismBlankAppCore"xmlns:prism="http://prismlibrary.com/"><Application.Resources /> </prism:PrismApplication> public partial class App{protected override Window CreateShell(){return Container.Resolve<MainWindow>();}protected override void RegisterTypes(IContainerRegistry containerRegistry){}}在App.xaml文件中,原先的Application類替換為了prism:PrismApplication,但同樣的它們都是App的部分類。那PrismApplication和Application有什么關(guān)系呢?
#mermaid-svg-gXA0uYitoD3iscW3 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-gXA0uYitoD3iscW3 .error-icon{fill:#552222;}#mermaid-svg-gXA0uYitoD3iscW3 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-gXA0uYitoD3iscW3 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-gXA0uYitoD3iscW3 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-gXA0uYitoD3iscW3 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-gXA0uYitoD3iscW3 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-gXA0uYitoD3iscW3 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-gXA0uYitoD3iscW3 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-gXA0uYitoD3iscW3 .marker.cross{stroke:#333333;}#mermaid-svg-gXA0uYitoD3iscW3 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-gXA0uYitoD3iscW3 g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-gXA0uYitoD3iscW3 g.classGroup text .title{font-weight:bolder;}#mermaid-svg-gXA0uYitoD3iscW3 .nodeLabel,#mermaid-svg-gXA0uYitoD3iscW3 .edgeLabel{color:#131300;}#mermaid-svg-gXA0uYitoD3iscW3 .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-gXA0uYitoD3iscW3 .label text{fill:#131300;}#mermaid-svg-gXA0uYitoD3iscW3 .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-gXA0uYitoD3iscW3 .classTitle{font-weight:bolder;}#mermaid-svg-gXA0uYitoD3iscW3 .node rect,#mermaid-svg-gXA0uYitoD3iscW3 .node circle,#mermaid-svg-gXA0uYitoD3iscW3 .node ellipse,#mermaid-svg-gXA0uYitoD3iscW3 .node polygon,#mermaid-svg-gXA0uYitoD3iscW3 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-gXA0uYitoD3iscW3 .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-gXA0uYitoD3iscW3 g.clickable{cursor:pointer;}#mermaid-svg-gXA0uYitoD3iscW3 g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-gXA0uYitoD3iscW3 g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-gXA0uYitoD3iscW3 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-gXA0uYitoD3iscW3 .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-gXA0uYitoD3iscW3 .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-gXA0uYitoD3iscW3 .dashed-line{stroke-dasharray:3;}#mermaid-svg-gXA0uYitoD3iscW3 #compositionStart,#mermaid-svg-gXA0uYitoD3iscW3 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gXA0uYitoD3iscW3 #compositionEnd,#mermaid-svg-gXA0uYitoD3iscW3 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gXA0uYitoD3iscW3 #dependencyStart,#mermaid-svg-gXA0uYitoD3iscW3 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gXA0uYitoD3iscW3 #dependencyStart,#mermaid-svg-gXA0uYitoD3iscW3 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gXA0uYitoD3iscW3 #extensionStart,#mermaid-svg-gXA0uYitoD3iscW3 .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gXA0uYitoD3iscW3 #extensionEnd,#mermaid-svg-gXA0uYitoD3iscW3 .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gXA0uYitoD3iscW3 #aggregationStart,#mermaid-svg-gXA0uYitoD3iscW3 .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gXA0uYitoD3iscW3 #aggregationEnd,#mermaid-svg-gXA0uYitoD3iscW3 .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-gXA0uYitoD3iscW3 .edgeTerminals{font-size:11px;}#mermaid-svg-gXA0uYitoD3iscW3 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}ApplictionPrismApplicationBase+IContainerProvider Container+Window CreateShell()PrismApplicationPrismApplictionBase抽象類中有幾個重要的屬性和方法:
- IContainerProvider Container只讀屬性:Unity容器對象,可以用來創(chuàng)建對象。
- abstract RegisterTypes(IContainerRegistry containerRegistry)抽象方法:可以用來給容器中注冊對象。
- abstract Window CreateShell()抽象方法:用來創(chuàng)建主窗體。
try containerRegistry)抽象方法:可以用來給容器中注冊對象。
- abstract Window CreateShell()抽象方法:用來創(chuàng)建主窗體。
所以我們可以看到在App類中,已經(jīng)重寫了CreateShell()方法和RegisterTypes()方法。還有一點值得一提,在App.xaml文件中,我們沒有設(shè)置StartupUri,這是因為我們重寫了CreateShell()方法,并在方法中使用容器創(chuàng)建了主窗體。
六 Demo
Demo持續(xù)更新中
總結(jié)
以上是生活随笔為你收集整理的WPF Prism框架介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 同人游戏开发工具巡礼——AVG ADV
- 下一篇: 值得尝试的 MQTT 客户端工具