C# 一个基于.NET Core3.1的开源项目帮你彻底搞懂WPF框架Prism
--概述
这个项目演示了如何在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编程大全
C# 一个基于.NET Core3.1的开源项目帮你彻底搞懂WPF框架Prism相关推荐
- 分享一个基于GPT-3.5 Turbo的开源项目,界面简洁大气,反应速度快
今天在github又发现一个国内的大神开源的chatGPT项目.先看看整体的效果如何吧. 这个项目是基于OpenAI GPT-3.5 Turbo API 的demo. 本地部署 环境准备 安装node ...
- Kimera:一个基于度量语义的SLAM开源库
标题:Kimera:an Open-Source Library for Real-Time Metric-Semantic Localization and Mapping 作者:Antoni Ro ...
- 自荐Mall4j项目一个基于spring boot的Java开源商城系统
前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样 ...
- java license 开源_MinIO:一个基于Apache License v2.0开源协议的对象存储服务
MinIO Quickstart Guide--快速入门 MinIO 是一个基于Apache License v2.0开源协议的对象存储服务.它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构 ...
- 一个基于.Net+Vue开发的开源权限工作流系统
今天给大家推荐一个开源权限工作流系统,一个快速开发框架. 项目简介 这是一个基于.Net 5开发的权限管理.工作流系统框架.借鉴了Martin Fowler企业级应用开发思想,框架选项都是使用最新的技 ...
- 基于STC51:四轴飞控开源项目原理图与源码(入门级DIY)
目录 前言(作者:宏晶科技) 一.飞控配件 二.接线 三.原理图 四.调试 五.程序 六.完整工程.原理图文件获取 前言(作者:宏晶科技) 本飞控仅仅是姿态飞行控制,没有GPS.电子罗盘.气压高度计. ...
- 这套ai的思维让我感到了一个细思极恐的开源项目
这套ai的思维让我感到了一个细思极恐的开源项目 去年,一款角色扮演游戏在国内市场悄然崛起,并在年轻人群体中得到了广泛传播,它有着一个响当当的的名字,叫「剧本杀」. 剧本杀玩法非常简单. 在游戏开始前, ...
- 微人事 star 数超 10k 啦!聊聊如何打造一个 star 数超 10k 的开源项目
看了下,微人事(https://github.com/lenve/vhr)项目 star 数超 10k 啦,松哥第一个 star 数过万的开源项目就这样诞生了. 两年前差不多就是现在这个时候,松哥所在 ...
- Cloudera Impala:基于Hadoop的实时查询开源项目
转载自: http://www.csdn.net/article/2012-10-25/2811151 Cloudera Impala:基于Hadoop的实时查询开源项目 发表于11小时前| 3663 ...
- 微人事 star 数超 10k,如何打造一个 star 数超 10k 的开源项目
看了下,微人事(https://github.com/lenve/vhr)项目 star 数超 10k 啦,松哥第一个 star 数过万的开源项目就这样诞生了. 两年前差不多就是现在这个时候,松哥所在 ...
最新文章
- 电子计算机之父冯.诺依曼的主要贡献,约翰·冯·诺依曼,约翰·冯·诺依曼的生平,贡献等...
- Something haunts me in Python
- 灯泡里的钨丝是怎么放进去的,这个视频解开我20多年的疑惑!
- 在C语言中023是八进制数,C语言总结
- zookeeper Error contacting service. It is probably not running
- Java的一些基础知识深入
- 工作中一些环境问题解决记录
- ubuntu安装宝塔界面
- 【火星传媒深度】Coinbase:加密世界的“谷歌”
- 学大数据需要具备四种条件?你具备几种?
- 使用java代码打印三角形、平行四边形、菱形
- 照片模糊怎么办?教你简单三步瞬间修复照片清晰度!
- 数字平原搭建赛博朋克风城市夜景
- 解决:Jackson反序列化Java内部类失败(序列化后的识别码为LinkedHashMap,而非内部类本身)
- 微型计算机原理与接口技术-实验一
- git代码合并了后发现有冲突,我们怎么取消合并?
- 在UTF-8下写字库
- 唐纳德和他的数学老师
- Android系统Crash/ANR类型弹框
- Linux——内存的申请与释放
热门文章
- WebM视频格式怎么转换成MP4
- 信号与系统分析中的复变函数
- MathType编辑器安装(写公式)
- python达梦数据库_Python 封装 DM 达梦 数据库操作(使用类封装基本的增删改查)...
- 火狐浏览器的hoxx附件还能用吗_Haspit
- node.js 快速入门
- 微信小程序记录v1.0
- 自然语言处理NLP星空智能对话机器人系列:理解语言的 Transformer 模型-子词分词器
- Go语言实战(一)环境配置
- [CSS] 详细解释 @media 属性与 (max-width:) and (min-width) 之间的关系及用法