wpf prisme对话服务(弹窗)代码笔记

一. 定义创建一个弹窗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>();

        }
    }
}
  1. 在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));

二. 打开弹窗并传参,弹窗关闭时触发回调函数,接收弹窗返回的值以及状态

  1. 打开弹窗
private void Open(string obj)
        {
       		 //obj:依赖注入的弹窗名
            dialogService.ShowDialog(obj);
         }
  1. 打开弹窗并传参,弹窗关闭时触发回调函数,接收弹窗返回的值以及状态
 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");
                }
            });
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值