--概述

这个项目演示了如何在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的更多相关文章

  1. 一个基于Net Core3.0的WPF框架Hello World实例

    目录 一个基于Net Core3.0的WPF框架Hello World实例 1.创建WPF解决方案 1.1 创建Net Core版本的WPF工程 1.2 指定项目名称,路径,解决方案名称 2. 依赖库 ...

  2. 我发起了一个 支持 ServerFul 架构 的 .Net 开源项目 ServerFulManager

    大家好,  我发起了一个 支持 ServerFul 架构 的 .Net 开源项目 ServerFulManager . ServerFulManager 的 目标 是 实现一个 支持 ServerFu ...

  3. 我发起了一个 .Net Core 平台上的 开源项目 ShadowDomain 用于 热更新

    大家好,  我发起了一个 .Net Core 平台上的 开源项目 ShadowDomain  用于 热更新 . 简单的说, 原理就是 类似 Asp.net 那样 让 当前 WebApp 运行在一个 A ...

  4. 新建一个基于vue.js+Mint UI的项目

    上篇文章里面讲到如何新建一个基于vue,js的项目(详细文章请戳用Vue创建一个新的项目). 该项目如果需要组件等都需要自己去写,今天就学习一下如何新建一个基于vue.js+Mint UI的项目,直接 ...

  5. 微人事 star 数超 10k,如何打造一个 star 数超 10k 的开源项目

    看了下,微人事(https://github.com/lenve/vhr)项目 star 数超 10k 啦,松哥第一个 star 数过万的开源项目就这样诞生了. 两年前差不多就是现在这个时候,松哥所在 ...

  6. 基于.NET Core的优秀开源项目合集

    开源项目非常适合入门,并且可以作为体系结构参考的好资源, GitHub中有几个开源的.NET Core项目,这些项目将帮助您使用不同类型的体系结构和编码模式来深入学习 .NET Core技术, 本文列 ...

  7. .NET Core/.NET5/.NET6 开源项目汇总9:客户端跨平台UI框架

    系列目录     [已更新最新开发文章,点击查看详细] .NET Core 实现了跨平台,支持在 Windwos.Linux.macOS上开发与部署,但是也仅限于Web应用程序.对于Windows桌面 ...

  8. Facebook开源项目:我们为什么要用Fresco框架?

    (Facebook开源项目)Fresco:一个新的Android图像处理类库 在Facebook的Android客户端上快速高效的显示图片是非常重要的.然而多年来,我们遇到了很多如何高效存储图片的问题 ...

  9. Linux 命令多到记不住?这个开源项目帮你一网打尽!

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 最近发现了一个 ...

随机推荐

  1. 微信小程序码生成及canvas绘制

    吐槽:某厂的开发文档写的跟屎一样 1.后台返回accessToken,小程序请求获取小程序码 uni.request({ url: 'https://api.weixin.qq.com/wxa/get ...

  2. MySQL优化之索引解析

    索引的本质 MySQL索引或者说其他关系型数据库的索引的本质就只有一句话,以空间换时间. 索引的作用 索引关系型数据库为了加速对表中行数据检索的(磁盘存储的)数据结构 索引的分类 数据结构上面的分类 ...

  3. AutoML论文调研

    AutoLearn - Automated Feature Generation and Selection - 2017 ICDM 核心思想: 用特征之间两两回归的方法,发现相关特征的额外信息. 主 ...

  4. jvm初步理解

    1.什么是运行时数据区? 1.什么是运行时数据区 ​ javac 指令:编译java文件生成class文件 ​ java指令:运行class文件即将数据放到jvm中 ​ class文件运行,后将不同的 ...

  5. 如何恢复 iCloud 已删除文件

    原文链接 问题 今天在查找之前的 C++ 笔记时,突然发现之前的资料全没了,整个 Cpp 文件夹内就只剩下了三个文件,怎么形容当时的心情呢,应该说是一下就跌倒了谷底,感觉之前的心血全白费了,有种深深的 ...

  6. OSPF协议原理及配置3-邻居关系的建立

    OSPF协议原理及配置3-邻居关系的建立   OSPF协议的邻居关系是通过交换Hello报文建立的,所以我们先对OSPF的报文的种类,及Hello报文的结构做一了解. 1 OSPF协议有5种协议报文( ...

  7. 机器学习实战 | SKLearn最全应用指南

    作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/41 本文地址:http://www.showmeai.tech/article-det ...

  8. 老徐和阿珍的故事:ArrayList和LinkedList的效率到底哪个高?

    人物背景: 老徐,男,本名徐福贵,从事Java相关研发工作多年,职场老油条,摸鱼小能手,虽然岁数不大但长的比较着急,人称老徐.据说之前炒某币败光了所有家产,甚至现在还有欠债. 阿珍,女,本名陈家珍,刚 ...

  9. idea导入gitee下载的项目文件

    前一段时间在学习javaWeb时想要把gitee中的下载的项目在本地环境中跑一遍,然后根据效果再自己做出来. 但是当导入到IDEA中,配置完tomcat后一直报404错误.404是学习javaweb阶 ...

  10. 现代中央处理器(CPU)是怎样进行分支预测的?

    人们一直追求CPU分支预测的准确率,论文Simultaneous Subordinate Microthreading (SSMT)中给了一组数据,如果分支预测的准确率是100%,大多数应用的IPC会 ...