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

8 篇文章 3 订阅
6 篇文章 3 订阅

一. 定义创建一个弹窗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
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WPF(Windows Presentation Foundation)是一种用于创建Windows应用程序的开发框架。而Prism是一种用于构建可扩展、模块化和可重用的WPF应用程序的开发框架。在WPF Prism中,弹窗可以通过对话框的方式来实现。 在WPF Prism中,可以使用对话服务DialogService)来创建和管理弹窗DialogService提供了一系列用于显示、关闭和传递参数给弹窗的方法。可以通过注册DialogService服务来在整个应用程序中使用。 要创建一个弹窗,首先需要定义一个弹窗的View和ViewModel。View通常是一个UserControl,用于定义弹窗的界面。ViewModel负责处理弹窗的逻辑和与数据的交互。 在需要显示弹窗的地方,可以使用DialogService的Show方法来显示弹窗。Show方法接收一个字符串参数来指定要显示的弹窗的名称,该名称应与弹窗的View名称相对应。还可以通过Show方法传递要传递给弹窗的参数。 在ViewModel中,可以通过实现INavigationAware接口来获取传递给弹窗的参数。这样,在弹窗显示后,ViewModel就可以使用这些参数来进行必要的操作。 当需要关闭弹窗时,可以使用DialogService的Close方法来关闭弹窗。Close方法接收一个字符串参数来指定要关闭的弹窗的名称,该名称应与弹窗的View名称相对应。 总之,WPF Prism提供了弹窗的管理和控制的机制,通过DialogService可以创建、显示和关闭弹窗,使得应用程序具有更好的用户体验和交互性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值