Dialog对话服务
创建弹窗控件
在View中创建用户控件:MsgView。然后创建一个业务层处理逻辑:MsgViewModel的类,类继承IDialogAware方法,实现如下接口
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Text;
namespace BlankCoreApp1.ViewModels
{
public class MsgViewModel :IDialogAware
{
public string Title
{
get { return title; }
set { title = value;RaisePropertyChanged(); }
}
public event Action<IDialogResult> RequestClose;
/// <summary>
/// 允许用户关闭窗口
/// </summary>
/// <returns></returns>
public bool CanCloseDialog()
{
return true;
}
/// <summary>
/// 用户关闭方法
/// </summary>
public void OnDialogClosed()
{
}
/// <summary>
/// 接收参数
/// </summary>
/// <param name="parameters"></param>
public void OnDialogOpened(IDialogParameters parameters)
{
}
}
}
实现手动关联
在App.xaml.cs中实现手动关联
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// 建立手动关联 "Question"为别名
containerRegistry.RegisterDialog<MsgView, MsgViewModel>("Question");
}
实现弹窗
在MainWindowViewModel中实现IDialogService服务
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Windows;
namespace BlankCoreApp1.ViewModels
{
public class MainWindowViewModel : BindableBase
{
private readonly IRegionManager regionManager;
private readonly IDialogService dialog; // 弹窗服务
IRegionNavigationJournal journal; // 导航日志的接口
public DelegateCommand OpenACommand { get; private set; }
public DelegateCommand OpenBCommand { get; private set; }
public DelegateCommand GoBackCommand { get; private set; }
public DelegateCommand GoForwordCommand { get; private set; }
public MainWindowViewModel(IRegionManager regionManager,IDialogService dialog)
{
OpenACommand = new DelegateCommand(OpenA);
OpenBCommand = new DelegateCommand(OpenB);
GoBackCommand = new DelegateCommand(GoBack);
GoForwordCommand = new DelegateCommand(GoForword);
this.regionManager = regionManager;
this.dialog = dialog;
}
private void GoForword()
{
journal.GoForward();
}
private void GoBack()
{
journal.GoBack();
}
private void OpenB()
{
// 设置区域内容为ViewB的内容。 使用回调函数arg来拿到导航日志
regionManager.RequestNavigate("ContentRegion", "ViewB", arg =>
{
journal = arg.Context.NavigationService.Journal;
});
}
private void OpenA()
{
// 弹窗
dialog.ShowDialog("Question");
}
}
}
实现参数传递
使用DialogParameters,键值对的方式传递参数
private void OpenA()
{
// 传递参数
DialogParameters param = new DialogParameters();
param.Add("Value", "Hello");
// 弹窗 arg为回调结果
dialog.ShowDialog("Question",param,arg=>
{
// 处理返回结果
if(arg.Result == ButtonResult.OK)
{
}
});
}
获取传递参数
在MsgViewModel中获取传递的参数
public void OnDialogOpened(IDialogParameters parameters)
{
var title = parameters.GetValue<string>("Value");
}
实现按钮命令绑定
创建两个按钮命令并进行初始化
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Text;
namespace BlankCoreApp1.ViewModels
{
public class MsgViewModel :BindableBase, IDialogAware
{
public MsgViewModel()
{
SaveCommand = new DelegateCommand(() =>
{
DialogParameters param = new DialogParameters();
param.Add("Value", Title);
RequestClose.Invoke(new DialogResult(ButtonResult.OK, param));
});
CancelCommand = new DelegateCommand(() =>
{
RequestClose.Invoke(new DialogResult(ButtonResult.No));
});
}
public DelegateCommand SaveCommand { get; set; }
public DelegateCommand CancelCommand { get; set; }
// 属性通知
private string title;
public string Title
{
get { return title; }
set { title = value;RaisePropertyChanged(); }
}
public event Action<IDialogResult> RequestClose;
/// <summary>
/// 允许用户关闭窗口
/// </summary>
/// <returns></returns>
public bool CanCloseDialog()
{
return true;
}
/// <summary>
/// 用户关闭方法
/// </summary>
public void OnDialogClosed()
{
}
/// <summary>
/// 接收参数
/// </summary>
/// <param name="parameters"></param>
public void OnDialogOpened(IDialogParameters parameters)
{
var title = parameters.GetValue<string>("Value");
}
}
}
实现绑定命令到按钮
在MsgView.xaml中对按钮实现命令绑定
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
FontSize="30"
Text="编辑窗口"
/>
<TextBox
Grid.Row="1"
Margin="5"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
FontSize="30"
Text="{Binding Title}"
/>
<StackPanel
Grid.Row="2"
HorizontalAlignment="Right"
Orientation="Horizontal">
<Button
Width="100"
Height="35"
Margin="10"
Command="{Binding SaveCommand}"
Content="确定"
FontSize="22"
/>
<Button
Width="100"
Height="35"
Margin="10"
Command="{Binding CancelCommand}"
Content="取消"
FontSize="22"
/>
</StackPanel>
</Grid>
实现返回结果判断
在回调函数中对result进行接收和判断。
dialog.ShowDialog("Question",param,arg=>
{
// 处理返回结果
if(arg.Result == ButtonResult.OK)
{
var value = arg.Parameters.GetValue<string>("Value");
MessageBox.Show($"用户输入了:{value}");
}
else
{
MessageBox.Show("用户取消了弹窗");
}
});
