调用者
using BlankApp1.Views;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Windows;
namespace BlankApp1.ViewModels
{
public class MainWindowViewModel : BindableBase
{
private readonly IRegionManager _regionManager;
private readonly IDialogService dialog;
public DelegateCommand OpenCommandB { get; set;}
public MainWindowViewModel(IRegionManager regionManager,IDialogService dialog)
{
OpenCommandB = new DelegateCommand(OpenB);
this._regionManager = regionManager;
this.dialog = dialog;
}
private void OpenB()
{
DialogParameters parm=new DialogParameters();
parm.Add("value", "Test1");
dialog.ShowDialog("MsgView",parm,arg =>
//arg 是一个在对话框关闭后通过回调函数传递的参数
//parm传给对话框的参数
{
if (arg.Result == ButtonResult.OK)
{
var value=arg.Parameters.GetValue<string>("Value");
//接收对话框的参数
MessageBox.Show($"用户输入了:{value}");
}
else
{
MessageBox.Show("用户取消了弹窗");
}
});
}
}
}
对话框
using DryIoc;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
namespace BlankApp1.ViewModels
{
public class MsgViewViewModel : BindableBase, IDialogAware
{
public DelegateCommand SaveCommand { get; set; }
public DelegateCommand CancleCommand { get; set; }
public event Action<IDialogResult> RequestClose;
public MsgViewViewModel(IDialogService dialogService)
{
SaveCommand = new DelegateCommand(() =>
{
DialogParameters parm=new DialogParameters();
parm.Add("Value", Title);
RequestClose?.Invoke(new DialogResult(ButtonResult.OK,parm));
//在对话框关闭后将信息传递给原界面;
});
CancleCommand = new DelegateCommand(() =>
{
RequestClose?.Invoke(new DialogResult(ButtonResult.No));
});
}
private string title;
public string Title
{
get { return title; }
set
{
title = value;
RaisePropertyChanged();
}
}
//对话框是否可以关闭
public bool CanCloseDialog()
{
return true;
}
public void OnDialogClosed()
{
}
public void OnDialogOpened(IDialogParameters parameters)
//拿到调用者传递的参数
{
var Title=parameters.GetValue<string>("value");
}
}
}