当前位置:网站首页>Prism dialog service
Prism dialog service
2022-07-16 08:04:00 【Hometown 2130】
Continue with the code in the previous section , Delete the redundant part of the code .
Dialogue service , It's a pop-up prompt ,prism It was packaged .
Business : Click button , Pass the value to the pop-up box , Pop up box prompt , Select OK or cancel in the pop-up box , Then send the value back to the main interface
1. The code structure

2.MainViewModel.cs
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;
using WPFPrism.Views;
namespace WPFPrism.ViewModels
{
public class MainViewModel : BindableBase
{
private readonly IDialogService dialogService;
public DelegateCommand<string> OpenCommand { get; set; }// Create command ,string Used to receive parameters , for example ViewA
public MainViewModel(IDialogService dialogService)// Click here for quick operation . Take the area defined by the interface Content
{
OpenCommand = new DelegateCommand<string>(Open);
this.dialogService = dialogService;
}
private void Open(string obj)
{
DialogParameters keys = new DialogParameters();
keys.Add("title", " Test cartridge ");// Click the button in the main interface to transfer the parameters to the pop-up interface
dialogService.ShowDialog(obj, keys, callback =>
{
if (callback.Result == ButtonResult.OK)
{
string result = callback.Parameters.GetValue<string>("value");// Receive the value passed
}
});
}
}
}
3.ViewDViewModel.cs
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPFPrism.ViewModels
{
public class ViewDViewModel : IDialogAware
{
public DelegateCommand CancelComm { get; set; }
public DelegateCommand OkComm { get; set; }
public string Title { get; set; }
public event Action<IDialogResult> RequestClose;
public ViewDViewModel()
{
CancelComm = new DelegateCommand(Cancel);
OkComm = new DelegateCommand(Ok);
}
/// <summary>
/// determine
/// </summary>
private void Ok()
{
//RequestClose?.Invoke(new DialogResult(ButtonResult.OK));// Or return OK
OnDialogClosed();
}
/// <summary>
/// close
/// </summary>
private void Cancel()
{
RequestClose?.Invoke(new DialogResult(ButtonResult.No));
}
public bool CanCloseDialog()
{
return true;
}
public void OnDialogClosed()
{
DialogParameters keys = new DialogParameters();
keys.Add("value", "hello");// Pass the value to the main interface
RequestClose?.Invoke(new DialogResult(ButtonResult.OK, keys));
}
public void OnDialogOpened(IDialogParameters parameters)
{
// First enter this
// Receive the parameters passed from the main interface
Title = parameters.GetValue<string>("title");
}
}
}
4. main interface

5. Popup interface

6.App.xaml.cs, In fact, this is just a sentence RegisterDialog, Register the pop-up box
using ModuleA;
using ModuleB;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WPFPrism.ViewModels;
using WPFPrism.Views;
namespace WPFPrism
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
// Get it through the container MainView, It is also the place to start
return Container.Resolve<MainView>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// The function of dependency injection , Add the content to be realized by dependency injection
//containerRegistry.RegisterForNavigation<ViewA>(); //containerRegistry.RegisterForNavigation<ViewA>("ViewA The specific name of ") Customize , It can be written like this ;
//containerRegistry.RegisterForNavigation<ViewB>();
//containerRegistry.RegisterForNavigation<ViewC>();// We still use C modular ,C The module is of the current program .
// Register the pop-up prompt interface
containerRegistry.RegisterDialog<ViewD, ViewDViewModel>();
}
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
// Here is the code
//moduleCatalog.AddModule<ModuleAfile>();// load A modular
//moduleCatalog.AddModule<ModuleBfile>();// load B modular
base.ConfigureModuleCatalog(moduleCatalog);
}
protected override IModuleCatalog CreateModuleCatalog()
{
// Here is the configuration
//return new DirectoryModuleCatalog()
//{
// ModulePath = @"Modules"// Here is the main program exe Same as... Under the folder Modules Folder
//};
return base.CreateModuleCatalog();
}
}
}
What you should pay attention to is 2 spot :
Receive the information of the main interface when opening the dialog box , To deal with .
When closing the dialog box, pass the information to the main interface .
边栏推荐
- nodejs+express设置和获取cookie,session
- Summary of word segmentation tools
- Basic use of gzip and PM2 management tools for project launch
- Full Permutation next_ Permutation() function
- 关于pycharm汉化 2020-09-20
- Day 8 of leetcode question brushing
- Huawei all connected mGRE and star topology mGRE (full mesh and non full mesh)
- TestCafe之定位、操作页面元素以及验证执行结果
- echarts+flask+mysql 从mysql 数据库中检索数据,进行异步报表展示
- Azkaban overview
猜你喜欢
随机推荐
Day 7 of leetcode question brushing
Day 6 of DL
数组或是对象、日期的操作
常用的正则表达式
echarts+flask+mysql 从mysql 数据库中检索数据,进行异步报表展示
Xiao Bai can understand tacotron's Chinese speech synthesis practice
GoFrame Step by Step Demo - P1
How to choose interface automation testing tools
CCF 202012-2 optimal threshold for period end forecast
Rasa3 domain官方文档翻译
C#笔记-基础知识,问答,WPF
Day 5 of leetcode question brushing
4年测试工程师经历,下一步转开发还是继续测试?
Are you still using the strategy mode to solve if else? Map+ functional interface method is yyds
华为全连MGRE与星型拓扑MGRE(全网状与非全网状)
Recursive implementation of exponential enumeration
Set、Map、WeakSet 和 WeakMap 的区别
CentOS 7X 安装Mysql 数据库
项目上线之gzip和pm2管理工具的基本使用
rasa action 服务并发









