C#-Wpf-Prism.DryIoc-【6】对话框服务

Posted on 2022-04-24 15:10  轻吟浅唱,蓦然花开  阅读(610)  评论(0编辑  收藏  举报

【1】先完成基本的环境搭建,可参考C#-WPF-Prism.DryIoc-【1】环境搭建 - 轻吟浅唱,蓦然花开 - 博客园 (cnblogs.com)

【2】项目基本结构如下图,目的:打开对话框传入参数并且得到返回值

【3】对话框的ViewModel继承IDialogAware并实现该接口

using Prism.Commands;
using Prism.Services.Dialogs;
using System;

namespace Wpf_Prism.DryIoc_DialogService.ViewModels.DialogViewModels
{
    public class HelloViewModel : IDialogAware
    {
        public HelloViewModel()
        {
            CloseDialogCommand = new DelegateCommand(CloseDialog);
        }
        private void CloseDialog()
        {
            //关闭并返回Result
            DialogParameters dialogParameters = new DialogParameters();
            dialogParameters.Add("Value", "Result");
            var result = new DialogResult(ButtonResult.OK, dialogParameters);
            RequestClose?.Invoke(result);
        }

        public DelegateCommand CloseDialogCommand { get;private set; }

        public string Title { get; set; } 

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
           
        }
        public void OnDialogOpened(IDialogParameters parameters)
        {
            //拿到参数
           Title = "Hello~"+parameters.GetValue<string>("Value");
        }
    }
}

【4】在App.xaml.cs 注册对话框服务以及要显示的对话框

using Prism.Ioc;
using Prism.Services.Dialogs;
using System.Windows;
using Wpf_Prism.DryIoc_DialogService.ViewModels.DialogViewModels;
using Wpf_Prism.DryIoc_DialogService.Views;
using Wpf_Prism.DryIoc_DialogService.Views.Dialogs;

namespace Wpf_Prism.DryIoc_DialogService
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        protected override Window CreateShell()
        {
           return Container.Resolve<MainView>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
           containerRegistry.Register<IDialogService, DialogService>();
            containerRegistry.RegisterDialog<HelloView, HelloViewModel>();
        }
    }
}

【5】通过在MainView的OpenDialog方法打开对话框并得到返回值

using Prism.Commands;
using Prism.Services.Dialogs;
using System.Windows;

namespace Wpf_Prism.DryIoc_DialogService.ViewModels
{
    public class MainViewModel
    {
        private readonly IDialogService dialogService;

        public DelegateCommand OpenDialogCommand { get; private set; }
        public MainViewModel(IDialogService dialogService)
        {
            this.dialogService = dialogService;
            OpenDialogCommand = new DelegateCommand(OpenDialog);
        }

        private void OpenDialog()
        {
            DialogParameters dialogParameters = new DialogParameters();
            dialogParameters.Add("Value", "MainView");
            dialogService.ShowDialog("HelloView", dialogParameters, callback =>
             {
                 if (callback.Parameters.ContainsKey("Value"))
                     MessageBox.Show(callback.Parameters.GetValue<string>("Value"));
             });
        }
    }
}

【6】运行项目效果如下

 

完成,谢谢!