Prism对话服务

50 篇文章 0 订阅
5 篇文章 0 订阅
本文详细介绍了如何在.NET中使用IDialogAware接口管理弹窗视图模型,包括窗口注册、事件处理和数据交互,以及如何在调用弹窗方法时传递参数和处理返回结果。
摘要由CSDN通过智能技术生成

弹窗的窗口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");
        }
    });
}

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值