5.Prism对话框IDialogAware
案例:
项目添加Prism框架引用。
1.添加对话框项目,添加一个用户控件ConfigView.xaml
<UserControl x:Class="ConfigModule.Views.ConfigView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" xmlns:local="clr-namespace:ConfigModule.Views" xmlns:prism="http://prismlibrary.com/" mc:Ignorable="d" Width="430" Height="500"> <Grid> <DockPanel HorizontalAlignment="Right" Grid.Row="1" Margin="0,5"> <Button Content="{DynamicResource 确定}" Command="{Binding OKCommand}" Width="80" Height="30" Margin="5" FontSize="14"/> <Button Content="{DynamicResource 取消}" IsCancel="True" Width="80" Height="30" Margin="5" FontSize="14"/> </DockPanel> </Grid> </UserControl>
2.添加一个Base基类,需要继承IDialogAware接口实现里面的方法(或vm中直接接触BindableBase, IDialogAware)。OnDialogOpened方法可以获取传过来的参数
public class DialogViewModelBase : BindableBase, IDialogAware { public string Title { get; set; } = "对话框标题"; public event Action<IDialogResult> RequestClose; /// <summary> /// 关闭对话框前,判断是否可以关闭 /// </summary> /// <returns></returns> /// <exception cref="NotImplementedException"></exception> public virtual bool CanCloseDialog() { return true; } /// <summary> /// 关闭时执行的方法 /// </summary> public virtual void OnDialogClosed() { } /// <summary> /// 打开时执行的方法 /// </summary> /// <param name="parameters"></param> /// <exception cref="NotImplementedException"></exception> public virtual void OnDialogOpened(IDialogParameters parameters) { } /// <summary> /// 关闭对话框 /// </summary> /// <param name="buttonResult"></param> /// <param name="dialogParameters"></param> protected virtual void CloseDialog(ButtonResult buttonResult, IDialogParameters dialogParameters = null) { RequestClose?.Invoke(new DialogResult(buttonResult, dialogParameters)); } }
3.添加ConfigViewModel类并继承DialogViewModelBase基类 。CloseDialog方法有两个参数,第一个是状态,第二个是参数,把参数传到父窗体中。
public class ConfigViewModel: DialogViewModelBase { public DelegateCommand OKCommand { get; } public ConfigViewModel(SystemConfigManager systemConfigManager, SystemConfigProvider systemConfigProvider) { OKCommand = new DelegateCommand(Submit); } private void Submit() { CloseDialog(Prism.Services.Dialogs.ButtonResult.OK); } }
使用:v
<MenuItem Header="系统设置" Command="{Binding SystemConfigCommand}"/>
vm
public class MenuViewModel: BindableBase { public DelegateCommand SystemConfigCommand { get;private set; } public MenuViewModel() { SystemConfigCommand = new DelegateCommand(SystemConfigAction); } private void SystemConfigAction() { DialogParameters parameters =new DialogParameters(); parameters.Add("ConfigViewKey", $"ConfigViewKey页面,现在时间{DateTime.Now.ToString("yyyy-MM-dd")}"); //弹窗并传入参数 PrismProvider.DialogService.ShowDialog(ViewNames.ConfigView, parameters, Callback); } private void Callback(IDialogResult result) { //todo...获取参数
var p= result.Parameters.GetValue<string>("key"); } }