WPF Prism.DryIoc(4)

当前介绍的是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 传递过来的参数
        }
    }
}

实现效果就是:能在程序中调用窗体弹窗最大程度的降低程序和视图之间的偶和性

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学软件开发的猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值