Prism中的Dialog(对话框)

调用者

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");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值