当前介绍的是prism Dryloc中的对话服务
能在程序中调用窗体弹窗最大程度的降低程序和视图之间的偶和性
步骤1 在Nuget包管理中下载Prism Dryloc 包
步骤2 在App.cs中设置如下
using Prism.DryIoc;
using Prism.Ioc;
using System.Windows;
namespace WpfApp1
{
public partial class App : PrismApplication
{
protected override Window CreateShell()
{//返回 其MainWindow 窗体 效果启动程序
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//注册窗体
containerRegistry.RegisterForNavigation<MainWindow,MainVM>();//指定 mainwindows的上下文
containerRegistry.RegisterDialog<ob1, ob1VM> ("A");//取名为A
}
}
}
步骤3 在主界面Xaml中做好布局显示,在主界面上设置打开弹窗的按钮button 设置主界面的上下文,做好绑定command 在构造函数声明参数 IRegionManager ,IDialogService 创建字段并赋值 其主界面的VM为MainVM 主界面为MainWindow 代码和Xaml如下
IRegionManager 命名空间为 usingPrism.Regions;
MainWindow绑定的上下文代码
using Prism.Commands;
using Prism.Mvvm;
using System.Windows.Input;
using Prism.Regions;
using Prism.Services.Dialogs;
using System.Windows;
namespace WpfApp1
{
public class MainVM:BindableBase
{
public MainVM(IRegionManager regionAdapter,IDialogService service)
{
RegionAdapter = regionAdapter;
Service = service;
}
public ICommand Open { get => new DelegateCommand<string>((p) => {
//RegionAdapter.Regions["x"].RequestNavigate(p);//普通 跳转用户控件
//x是刚显示的名称
//实现弹窗显示
//我们在打开弹窗的时候就 传递参数过去
DialogParameters keyValuePairs = new DialogParameters() { { "Title", "Value" } };
Service.ShowDialog(p, keyValuePairs, callback =>
{
if (callback.Result == ButtonResult.Yes)//程序在invoke 了会第一时间来到这判断
{
var p= callback.Parameters.GetValue<string>("Value"); //想获得刚刚传递的参数
MessageBox.Show("蟹蟹yes");
}
else MessageBox.Show("NO");
});
}); }
public IRegionManager RegionAdapter { get; }
public IDialogService Service { get; }
}
}
MainWindow 前端Xaml
<Window
x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<!-- 设计 -->
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Button
Width="50"
Height="40"
Command="{Binding Open}"
CommandParameter="A"
Content="打开弹窗" />
<!-- x为容器名字 -->
<ContentControl Grid.Row="1" prism:RegionManager.RegionName="x" />
</Grid>
</Window>
步骤4 创建被弹窗的用户控件 (window窗体不行)在用户控件所绑定的上下文类必须继承接口IDialogAware 命名空间 为 using Prism.Services.Dialogs; 其创建的弹窗代码和xaml布局代码如下
<UserControl
x:Class="WpfApp1.ob1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="450"
d:DesignWidth="800"
Background="Orange"
mc:Ignorable="d">
<!-- 写点用户交互 -->
<Grid >
<StackPanel Orientation="Horizontal">
<Button Content="确定" Width="150" Command="{Binding save}" Height="20"/>
<Button Content="取消" Width="150" Height="20" Command="{Binding quxiao}"/>
</StackPanel>
</Grid>
</UserControl>
被创建的用户控件弹窗视图如下
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Windows.Input;
namespace WpfApp1
{
public class ob1VM : IDialogAware
{
public string Title { get; set; }
public event Action<IDialogResult> RequestClose;
public ICommand save { get => new DelegateCommand(() => {
RequestClose.Invoke(new DialogResult(ButtonResult.Yes, new DialogParameters() { { "Value", "选中的yes" } }));
}); }
public ICommand quxiao
{
get => new DelegateCommand(() => {
RequestClose.Invoke(new DialogResult(ButtonResult.No, new DialogParameters() { { "Value", "选中的NO" } }));
});
}
public bool CanCloseDialog()
{
return true;//能否关闭窗体
}
public void OnDialogClosed()
{
}
public void OnDialogOpened(IDialogParameters parameters)
{
//parameters 接收参数
var o= parameters.GetValue<string>("Title");//键值对
//o 传递过来的参数
}
}
}
实现效果就是:能在程序中调用窗体弹窗最大程度的降低程序和视图之间的偶和性