当前位置:网站首页>prism发布订阅
prism发布订阅
2022-07-13 18:09:00 【故里2130】
延续上一节代码继续,多余部分代码进行删减。
一个地方发布消息,另一个地方接收消息。
也可以一个地方发布消息,多个地方接收消息。
主要是夸模块发送消息,进行通信的应用
业务:点击取消按钮的时候,在当前界面和主界面都弹窗口。
1.代码结构

2.MessageEvent.cs
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPFPrism.Event
{
public class MessageEvent : PubSubEvent<string>//消息传递string类型
{
//这里是其他界面要引用的地方,删除会报错
//例子是传递string类型的。这里还可以定义复杂的对象,进行传递
}
}
3. MainViewModel.cs
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WPFPrism.Event;
using WPFPrism.Views;
namespace WPFPrism.ViewModels
{
public class MainViewModel : BindableBase
{
private readonly IDialogService dialogService;
private readonly IEventAggregator aggregator;
public DelegateCommand<string> OpenCommand { get; set; }//创建命令,string用来接收参数,例如ViewA
public MainViewModel(IDialogService dialogService,IEventAggregator aggregator)//这里点击快速操作。拿界面定义的区域Content
{
OpenCommand = new DelegateCommand<string>(Open);
this.dialogService = dialogService;
this.aggregator = aggregator;
aggregator.GetEvent<MessageEvent>().Subscribe(SubMessage);
}
private void SubMessage(string obj)
{
MessageBox.Show($"我是主界面接收的消息:{obj}");
}
private void Open(string obj)
{
DialogParameters keys = new DialogParameters();
keys.Add("title", "测试弹框");//主界面点击按钮把参数传递给弹框界面
dialogService.ShowDialog(obj, keys, callback =>
{
if (callback.Result == ButtonResult.OK)
{
string result = callback.Parameters.GetValue<string>("value");//接收传递过来的值
}
});
}
}
}
4.ViewDViewModel.cs
using Prism.Commands;
using Prism.Events;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WPFPrism.Event;
namespace WPFPrism.ViewModels
{
public class ViewDViewModel : IDialogAware
{
private readonly IEventAggregator aggregator;
public DelegateCommand CancelComm { get; set; }
public DelegateCommand OkComm { get; set; }
public string Title { get; set; }
public event Action<IDialogResult> RequestClose;
public ViewDViewModel(IEventAggregator aggregator)
{
CancelComm = new DelegateCommand(Cancel);
OkComm = new DelegateCommand(Ok);
this.aggregator = aggregator;
aggregator.GetEvent<MessageEvent>().Subscribe(SubMessage);
}
private void SubMessage(string obj)
{
MessageBox.Show($"我是当前界面接收的消息:{obj}");
aggregator.GetEvent<MessageEvent>().Unsubscribe(SubMessage);//取消订阅,之后再点击就没有反应了
}
/// <summary>
/// 确定
/// </summary>
private void Ok()
{
//RequestClose?.Invoke(new DialogResult(ButtonResult.OK));//或者返回OK
OnDialogClosed();
}
/// <summary>
/// 关闭
/// </summary>
private void Cancel()
{
//向MessageEvent发布一个 123 的消息
aggregator.GetEvent<MessageEvent>().Publish("123111");
//RequestClose?.Invoke(new DialogResult(ButtonResult.No));
}
public bool CanCloseDialog()
{
return true;
}
public void OnDialogClosed()
{
DialogParameters keys = new DialogParameters();
keys.Add("value", "hello");//把值传递给主界面
RequestClose?.Invoke(new DialogResult(ButtonResult.OK, keys));
}
public void OnDialogOpened(IDialogParameters parameters)
{
//首先进入这个
//接收主界面传递过来的参数
Title = parameters.GetValue<string>("title");
}
}
}
5.主界面

6.弹框界面

7.App.xaml.cs不变
using ModuleA;
using ModuleB;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WPFPrism.ViewModels;
using WPFPrism.Views;
namespace WPFPrism
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
//通过容器去拿MainView,也是启动的地方
return Container.Resolve<MainView>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//依赖注入的功能,增加依赖注入要实现的内容
//containerRegistry.RegisterForNavigation<ViewA>(); //containerRegistry.RegisterForNavigation<ViewA>("ViewA的具体名字")自定义,可以这样写;
//containerRegistry.RegisterForNavigation<ViewB>();
//containerRegistry.RegisterForNavigation<ViewC>();//我们依然使用C模块,C模块是当前程序的。
//注册弹框提示的界面
containerRegistry.RegisterDialog<ViewD, ViewDViewModel>();
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
//这里是走代码的
//moduleCatalog.AddModule<ModuleAfile>();//加载A模块
//moduleCatalog.AddModule<ModuleBfile>();//加载B模块
base.ConfigureModuleCatalog(moduleCatalog);
}
protected override IModuleCatalog CreateModuleCatalog()
{
//这里是走配置的
//return new DirectoryModuleCatalog()
//{
// ModulePath = @"Modules"//这里是主程序exe同文件夹下面的Modules文件夹
//};
return base.CreateModuleCatalog();
}
}
}
边栏推荐
猜你喜欢

自动备份MySQL。且保留7天案例

Longest ascending subsequence longest common subsequence maximum field and longest non repeating subsequence

Day 8 of leetcode question brushing

Linux下安装单机版redis

还在用策略模式解决 if-else?Map+函数式接口方法才是YYDS

nacos安装教程

Redis只能做缓存?太out了!

程序猿专属“压测工具”并发模拟

MySQL learning records

01 knapsack filling form implementation
随机推荐
vscode更新时,报错 Failed to install visual Studio Code update. Updates may fail due to anti-virus softwa
MySQL learning records
Principe jsonp
Edge calculation kubeedge+edgemash
Why can't we use redis expiration monitoring to close orders?
XPath ultra detailed summary
主从复制读写分离保姆级教学
边缘计算 KubeEdge+EdgeMash
Wechat native payment
File management - Alibaba cloud OSS learning (I)
数制转换与子网划分
4年测试工程师经历,下一步转开发还是继续测试?
How to apply @transactional transaction annotation to perfection?
WordPress Chinese website code download
Is it true that you can't do software testing until you're 35? So what should I do I'm 36
The experience of finding a job after coming out of the software testing training class taught me these five things
Basic principle and configuration of switch
wordpress中文网站代码下载
C notes -c 7.0 essential
Day 13 of leetcode + day 3 of DL