--概述
這個專案演示瞭如何在WPF中使用各種Prism功能的示例。如果您剛剛開始使用Prism,建議您從第一個示例開始,按順序從列表中開始。每個示例都基於前一個示例的概念。
此專案平臺框架:.NET Core 3.1
Prism版本:8.0.0.1909
提示:這些專案都在同一解決方法下,需要依次開啟執行,可以選中專案-》右鍵-》設定啟動專案,然後執行:
目錄介紹
Topic | 描述 |
Bootstrapper and the Shell | 建立一個基本的載入程式和shell |
Regions | 建立一個區域 |
Custom Region Adapter | 為StackPanel建立自定義區域介面卡 |
View Discovery | 使用檢視發現自動注入檢視 |
View Injection | 使用檢視注入手動新增和刪除檢視 |
View Activation/Deactivation | 手動啟用和停用檢視 |
Modules with App.config | 使用應用載入模組。配置檔案 |
Modules with Code | 使用程式碼載入模組 |
Modules with Directory | 從目錄載入模組 |
Modules loaded manually | 使用IModuleManager手動載入模組 |
ViewModelLocator | 使用ViewModelLocator |
ViewModelLocator - Change Convention | 更改ViewModelLocator命名約定 |
ViewModelLocator - Custom Registrations | 為特定檢視手動註冊ViewModels |
DelegateCommand | 使用DelegateCommand和DelegateCommand<T> |
CompositeCommands | 瞭解如何使用CompositeCommands作為單個命令呼叫多個命令 |
IActiveAware Commands | 使您的命令IActiveAware僅呼叫啟用的命令 |
Event Aggregator | 使用IEventAggregator |
Event Aggregator - Filter Events | 訂閱事件時篩選事件 |
RegionContext | 使用RegionContext將資料傳遞到巢狀區域 |
Region Navigation | 請參見如何實現基本區域導航 |
Navigation Callback | 導航完成後獲取通知 |
Navigation Participation | 通過INavigationAware瞭解檢視和檢視模型導航參與 |
Navigate to existing Views | 導航期間控制檢視例項 |
Passing Parameters | 將引數從檢視/檢視模型傳遞到另一個檢視/檢視模型 |
Confirm/cancel Navigation | 使用IConfirmNavigationReqest介面確認或取消導航 |
Controlling View lifetime | 使用IRegionMemberLifetime自動從記憶體中刪除檢視 |
Navigation Journal | 瞭解如何使用導航日誌 |
部分專案演示和介紹
① BootstrapperShell啟動介面:
這個主要演示Prism框架搭建的用法:
step1:在nuget上引用Prsim.Unity
step2:修改App.xaml:設定載入程式
<Application x:Class="BootstrapperShell.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:BootstrapperShell"> <Application.Resources> </Application.Resources> </Application>
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var bootstrapper = new Bootstrapper(); bootstrapper.Run(); } }
step3:在載入程式中設定啟動專案:
using Unity; using Prism.Unity; using BootstrapperShell.Views; using System.Windows; using Prism.Ioc; namespace BootstrapperShell { class Bootstrapper : PrismBootstrapper { protected override DependencyObject CreateShell() { return Container.Resolve<MainWindow>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { } } }
step4:在MainWindow.xaml中顯示個字串
<Window x:Class="BootstrapperShell.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Shell" Height="350" Width="525"> <Grid> <ContentControl Content="Hello from Prism" /> </Grid> </Window>
②ViewInjection:檢視註冊
MainWindow.xaml:通過ContentControl 關聯檢視
<Window x:Class="ViewInjection.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" Title="Shell" Height="350" Width="525"> <DockPanel LastChildFill="True"> <Button DockPanel.Dock="Top" Click="Button_Click">Add View</Button> <ContentControl prism:RegionManager.RegionName="ContentRegion" /> </DockPanel> </Window>
MainWindow.xaml.cs:滑鼠點選後通過IRegion 介面註冊檢視
public partial class MainWindow : Window { IContainerExtension _container; IRegionManager _regionManager; public MainWindow(IContainerExtension container, IRegionManager regionManager) { InitializeComponent(); _container = container; _regionManager = regionManager; } private void Button_Click(object sender, RoutedEventArgs e) { var view = _container.Resolve<ViewA>(); IRegion region = _regionManager.Regions["ContentRegion"]; region.Add(view); } }
③ActivationDeactivation:檢視啟用和登出
MainWindow.xaml.cs:這裡在窗體建構函式中注入了一個容器擴充套件介面和一個regin管理器介面,分別用來裝載檢視和註冊regin,窗體的啟用和去啟用分別通過regions的Activate和Deactivate方法實現
public partial class MainWindow : Window { IContainerExtension _container; IRegionManager _regionManager; IRegion _region; ViewA _viewA; ViewB _viewB; public MainWindow(IContainerExtension container, IRegionManager regionManager) { InitializeComponent(); _container = container; _regionManager = regionManager; this.Loaded += MainWindow_Loaded; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { _viewA = _container.Resolve<ViewA>(); _viewB = _container.Resolve<ViewB>(); _region = _regionManager.Regions["ContentRegion"]; _region.Add(_viewA); _region.Add(_viewB); } private void Button_Click(object sender, RoutedEventArgs e) { //activate view a _region.Activate(_viewA); } private void Button_Click_1(object sender, RoutedEventArgs e) { //deactivate view a _region.Deactivate(_viewA); } private void Button_Click_2(object sender, RoutedEventArgs e) { //activate view b _region.Activate(_viewB); } private void Button_Click_3(object sender, RoutedEventArgs e) { //deactivate view b _region.Deactivate(_viewB); } }
④UsingEventAggregator:事件釋出訂閱
事件類定義:
public class MessageSentEvent : PubSubEvent<string> { }
註冊兩個元件:ModuleA和ModuleB
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog) { moduleCatalog.AddModule<ModuleA.ModuleAModule>(); moduleCatalog.AddModule<ModuleB.ModuleBModule>(); }
ModuleAModule 中註冊檢視MessageView
public class ModuleAModule : IModule { public void OnInitialized(IContainerProvider containerProvider) { var regionManager = containerProvider.Resolve<IRegionManager>(); regionManager.RegisterViewWithRegion("LeftRegion", typeof(MessageView)); } public void RegisterTypes(IContainerRegistry containerRegistry) { } }
MessageView.xaml:檢視中給button俺妞妞繫結命令
<UserControl x:Class="ModuleA.Views.MessageView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" Padding="25"> <StackPanel> <TextBox Text="{Binding Message}" Margin="5"/> <Button Command="{Binding SendMessageCommand}" Content="Send Message" Margin="5"/> </StackPanel> </UserControl>
MessageViewModel.cs:在vm中把介面繫結的命令委託給SendMessage,然後在方法SendMessage中釋出訊息:
using Prism.Commands; using Prism.Events; using Prism.Mvvm; using UsingEventAggregator.Core; namespace ModuleA.ViewModels { public class MessageViewModel : BindableBase { IEventAggregator _ea; private string _message = "Message to Send"; public string Message { get { return _message; } set { SetProperty(ref _message, value); } } public DelegateCommand SendMessageCommand { get; private set; } public MessageViewModel(IEventAggregator ea) { _ea = ea; SendMessageCommand = new DelegateCommand(SendMessage); } private void SendMessage() { _ea.GetEvent<MessageSentEvent>().Publish(Message); } } }
在MessageListViewModel 中接收並顯示接收到的訊息:
public class MessageListViewModel : BindableBase { IEventAggregator _ea; private ObservableCollection<string> _messages; public ObservableCollection<string> Messages { get { return _messages; } set { SetProperty(ref _messages, value); } } public MessageListViewModel(IEventAggregator ea) { _ea = ea; Messages = new ObservableCollection<string>(); _ea.GetEvent<MessageSentEvent>().Subscribe(MessageReceived); } private void MessageReceived(string message) { Messages.Add(message); } }
以上就是這個開源專案比較經典的幾個入門例項,其它就不展開講解了,有興趣的可以下載原始碼自己閱讀學習。
原始碼下載
github訪問速度較慢,所以我下載了一份放到的百度網盤
百度網盤連結:https://pan.baidu.com/s/10Gyks2w-R4B_3z9Jj5mRcA
提取碼:0000
---------------------------------------------------------------------
開源專案連結:https://github.com/PrismLibrary/Prism-Samples-Wpf
技術群:新增小編微信並備註進群
小編微信:mm1552923 公眾號:dotNet程式設計大全