弹窗的窗口ViewModel需要使用IDialogAware;这个接口会带上一个RequestClose的Action,作用就是返回窗体的值
这里的弹窗是窗体来的
使用RegisterDialog进行注册:
containerRegistry.RegisterDialog<ViewC, ViewCViewModel>();
弹窗样例:
public class ViewCViewModel : IDialogAware
{
public DelegateCommand CancelCommand { get; set; }
public DelegateCommand SaveCommand { get; set; }
public ViewCViewModel()
{
CancelCommand = new DelegateCommand(Cancel);
SaveCommand = new DelegateCommand(Save);
}
private void Save()
{
OnDialogClosed();
}
private void Cancel()
{
RequestClose?.Invoke(new DialogResult(ButtonResult.No));
}
public string Title { get; set; }
public event Action<IDialogResult> RequestClose;
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"); // 接收传递过来给窗口的参数
}
public bool CanCloseDialog()
{
return true;
}
}
然后这个返回值给调用弹窗的方法:
private void Open(string obj)
{
DialogParameters keys = new DialogParameters();
keys.Add("Title", "测试弹窗");
dialogService.ShowDialog(obj, keys, callback =>
{
// 弹窗点击ok就进来
if (callback.Result == ButtonResult.OK)
{
string result = callback.Parameters.GetValue<string>("Value");
}
});
}