<>一. 定义创建一个弹窗View,以及绑定ViewModel,配置ViewModel

1.创建一个弹窗View,以及绑定ViewModel
右键添加用户控件

创建ViewModel类
右键添加类

ViewC注入以及绑定ViewModel类
using ModuleA.ViewModels; using ModuleA.Views; using Prism.Ioc; using Prism.
Modularity; using System; using System.Collections.Generic; using System.Linq;
using System.Text; using System.Threading.Tasks; namespace ModuleA { public
class ModuleAProFile : IModule { public void OnInitialized(IContainerProvider
containerProvider) { } public void RegisterTypes(IContainerRegistry
containerRegistry) { //注入绑定viewmodel containerRegistry.RegisterForNavigation<
ViewC, ViewCViewModel>(); } } }
* 在ViewModel继承IDialogAware,继承实现下面方法
下面为全部代码 using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs;
using System; using System.Collections.Generic; using System.Linq; using System.
Text; using System.Threading.Tasks; namespace ModuleA.ViewModels { internal
class ViewCViewModel : IDialogAware { public string Title { get; set; } public
event Action<IDialogResult> RequestClose; //自己定义的取消关闭弹窗方法 public DelegateCommand
CancelCommand{ get; set; } //自己定义的确认关闭弹窗方法 public DelegateCommand SaveCommand {
get; set; } public ViewCViewModel() { CancelCommand = new DelegateCommand(Cancel
); SaveCommand = new DelegateCommand(Save); } //确认关闭弹窗,弹窗返回true private void
Save() { OnDialogClosed(); } //取消关闭弹窗,弹窗返回False private void Cancel() {
RequestClose?.Invoke(new DialogResult(ButtonResult.No)); } //是否允许被关闭 public bool
CanCloseDialog() { return true; } public void OnDialogClosed() {
DialogParameters keys=new DialogParameters(); keys.Add("values", "Hello");
//ButtonResult.OK确定按钮,弹窗状态返回true,keys弹出传回的参数 RequestClose?.Invoke(new
DialogResult(ButtonResult.OK, keys)); } //接收参数方法 public void OnDialogOpened(
IDialogParameters parameters) { Title = parameters.GetValue<string>("test"); } }
}
弹窗接收参数
//接收参数方法 public void OnDialogOpened(IDialogParameters parameters) {
//取键为test的参数值 Title = parameters.GetValue<string>("test"); }
弹窗关闭返回状态以及参数
DialogParameters keys=new DialogParameters(); keys.Add("values", "Hello");
//ButtonResult.OK确定按钮,弹窗状态返回true,ButtonResult.No取消按钮,弹窗状态返回false,keys弹出传回的参数
RequestClose?.Invoke(new DialogResult(ButtonResult.OK, keys));
<>二. 打开弹窗并传参,弹窗关闭时触发回调函数,接收弹窗返回的值以及状态

* 打开弹窗 private void Open(string obj) { //obj:依赖注入的弹窗名 dialogService.ShowDialog
(obj); }
* 打开弹窗并传参,弹窗关闭时触发回调函数,接收弹窗返回的值以及状态 private void Open(string obj) {
DialogParameters keys = new DialogParameters(); keys.Add("test", "hello");
//打开弹窗并传参,obj:打开的view名,keys:要传的参数,callback:回调函数,弹窗关闭后触发,返回的参数以及状态都在这获取
dialogService.ShowDialog(obj, keys, callback => { //判断弹窗返回true或false if (
callback.Result == ButtonResult.OK) { //返回ture则获取弹窗返回的键名称为value的值 string txt=
callback.Parameters.GetValue<string>("vlaue"); } }); }
全部代码
using Prism.Commands; 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; namespace Prism模块化Dome.
ViewModels{ public class MainViewModel: BindableBase { private readonly
IDialogService dialogService; public DelegateCommand<string> OpenCommand { get;
private set; } public MainViewModel(IDialogService dialogService) { this.
dialogService= dialogService; OpenCommand = new DelegateCommand<string>(Open); }
//打开弹窗 private void Open(string obj) { DialogParameters keys = new
DialogParameters(); keys.Add("test", "hello"); dialogService.ShowDialog(obj);
//打开弹窗并传参,obj:打开的view名,keys:要传的参数,callback:回调函数,弹窗关闭后触发,返回的参数以及状态都在这获取
dialogService.ShowDialog(obj, keys, callback => { //判断弹窗返回true或false if (
callback.Result == ButtonResult.OK) { //获取弹窗返回的值 string txt=callback.Parameters.
GetValue<string>("vlaue"); } }); } } }

技术
今日推荐
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:766591547
关注微信