日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 一个基于.NET Core3.1的开源项目帮你彻底搞懂WPF框架Prism

發(fā)布時間:2023/12/14 C# 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 一个基于.NET Core3.1的开源项目帮你彻底搞懂WPF框架Prism 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

--概述

這個項目演示了如何在WPF中使用各種Prism功能的示例。如果您剛剛開始使用Prism,建議您從第一個示例開始,按順序從列表中開始。每個示例都基于前一個示例的概念。

此項目平臺框架:.NET Core 3.1

Prism版本:8.0.0.1909

提示:這些項目都在同一解決方法下,需要依次打開運行,可以選中項目-》右鍵-》設(shè)置啟動項目,然后運行:

目錄介紹

Topic描述
Bootstrapper and the Shell創(chuàng)建一個基本的引導(dǎo)程序和shell
Regions創(chuàng)建一個區(qū)域
Custom Region Adapter為StackPanel創(chuàng)建自定義區(qū)域適配器
View Discovery使用視圖發(fā)現(xiàn)自動注入視圖
View Injection使用視圖注入手動添加和刪除視圖
View Activation/Deactivation手動激活和停用視圖
Modules with App.config使用應(yīng)用加載模塊。配置文件
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作為單個命令調(diào)用多個命令
IActiveAware Commands使您的命令I(lǐng)ActiveAware僅調(diào)用激活的命令
Event Aggregator使用IEventAggregator
Event Aggregator - Filter Events訂閱事件時篩選事件
RegionContext使用RegionContext將數(shù)據(jù)傳遞到嵌套區(qū)域
Region Navigation請參見如何實現(xiàn)基本區(qū)域?qū)Ш?/td>
Navigation Callback導(dǎo)航完成后獲取通知
Navigation Participation通過INavigationAware了解視圖和視圖模型導(dǎo)航參與
Navigate to existing Views導(dǎo)航期間控制視圖實例
Passing Parameters將參數(shù)從視圖/視圖模型傳遞到另一個視圖/視圖模型
Confirm/cancel Navigation使用IConfirmNavigationReqest界面確認或取消導(dǎo)航
Controlling View lifetime使用IRegionMemberLifetime自動從內(nèi)存中刪除視圖
Navigation Journal了解如何使用導(dǎo)航日志

部分項目演示和介紹

① BootstrapperShell啟動界面:

這個主要演示Prism框架搭建的用法:

step1:在nuget上引用Prsim.Unity

step2:修改App.xaml:設(shè)置引導(dǎo)程序

<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:在引導(dǎo)程序中設(shè)置啟動項目:

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 關(guān)聯(lián)視圖

<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:這里在窗體構(gòu)造函數(shù)中注入了一個容器擴展接口和一個regin管理器接口,分別用來裝載視圖和注冊regin,窗體的激活和去激活分別通過regions的Activate和Deactivate方法實現(xiàn)

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:事件發(fā)布訂閱

事件類定義:

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中發(fā)布消息:

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

以上就是這個開源項目比較經(jīng)典的幾個入門實例,其它就不展開講解了,有興趣的可以下載源碼自己閱讀學習。

源碼下載

github訪問速度較慢,所以我下載了一份放到的百度網(wǎng)盤

百度網(wǎng)盤鏈接:https://pan.baidu.com/s/10Gyks2w-R4B_3z9Jj5mRcA?

提取碼:0000

---------------------------------------------------------------------

開源項目鏈接:https://github.com/PrismLibrary/Prism-Samples-Wpf

技術(shù)群:添加小編微信并備注進群

小編微信:mm1552923 ??

公眾號:dotNet編程大全? ??

總結(jié)

以上是生活随笔為你收集整理的C# 一个基于.NET Core3.1的开源项目帮你彻底搞懂WPF框架Prism的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。