当前位置: 首页 > article >正文

wpf prism 《3》 弹窗 IOC

传统的弹窗 这种耦合度高

new 窗体() . Show();
new 窗体() . ShowDialog();

利用Prism 自动的 IOC 弹窗的 必须 必须 必须 页面控件

在这里插入图片描述
弹窗的 必须 必须 必须 页面控件
弹窗的 必须 必须 必须 页面控件
弹窗的 必须 必须 必须 页面控件
弹窗的 必须 必须 必须 页面控件
》》否则 报上面的错误

在这里插入图片描述
在这里插入图片描述
》》主程序

<Window x:Class="BlankApp2.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="10">
            <Button Content="弹窗1" Command="{Binding OpenCommand}" CommandParameter="Popup"></Button>
            <Button Content="弹窗2" Command="{Binding OpenCommand}" CommandParameter="UCPopup"></Button>
            <!--<Button Content="模块Student" Command="{Binding OpenCommand}" CommandParameter="ViewXX"></Button>
            <Button Content="模块C" Command="{Binding OpenCommand}" CommandParameter="ViewC"></Button>
            <Button Content="回退" Command="{Binding BackCommand}"></Button>-->
        </StackPanel>
        <ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion" />
    </Grid>
</Window>

》》主程序对应的 ViewModel

using Prism.Commands;
using Prism.Dialogs;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Navigation.Regions;
using System;
namespace BlankApp2.ViewModels
{
    public class MainViewModel : BindableBase
    {
        private string _title = "Prism Application";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        public DelegateCommand<string> OpenCommand { get; private set; }
        public IDialogService DialogService { get; }
        public MainViewModel(IDialogService dialogService)
        {
            this.DialogService = dialogService;
            this.OpenCommand = new DelegateCommand<string>(Open);

        }
        private void Open(string obj)
        {
            //传递给弹窗的参数信息
            DialogParameters keys = new DialogParameters();
            keys.Add("zen", "============zen============");
            DialogService.ShowDialog(obj, keys, callback =>
            {
                if (callback.Result == ButtonResult.OK)
                {   
                   //弹窗传递的参数信息
                    string ss = callback.Parameters.GetValue<string>("Info");
                }
            });
        }


    }
}

》》》弹窗用户控件 、弹窗的ViewModel
在这里插入图片描述

<UserControl x:Class="BlankApp2.Views.UCPopup"
             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:local="clr-namespace:BlankApp2.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="80"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock Text="{Binding Title}"></TextBlock>
        <TextBlock Text="弹窗信息" FontSize="40" Grid.Row="1"></TextBlock>
        <StackPanel Orientation="Horizontal" Margin="10" HorizontalAlignment="Right" Grid.Row="2">
            <Button Content="确     定" Margin="10" Command="{Binding OKCommand}"></Button>
            <Button Content="取     消" Margin="10" Command="{Binding CancelCommand}"></Button>
        </StackPanel>
    </Grid>
</UserControl>

using Prism.Commands;
using Prism.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlankApp2.ViewModels
{
    public class UCPopupViewModel : IDialogAware
    {
        public string Title { get; set; }
        public DialogCloseListener RequestClose { get; set; }
        public DelegateCommand CancelCommand { get; set; }
        public DelegateCommand OKCommand { get; set; }
        public UCPopupViewModel()
        {
            CancelCommand = new DelegateCommand(Cancel);
            OKCommand = new DelegateCommand(OKcmd);
        }
        private void OKcmd()
        {
            DialogParameters keys = new DialogParameters();
            keys.Add("Info", "======INDO==========");
            RequestClose.Invoke(keys, ButtonResult.OK);
        }

        private void Cancel()
        {
            RequestClose.Invoke(ButtonResult.Cancel);
        }
        //是否准许关闭弹窗
        public bool CanCloseDialog()
        {
            return true;
        }
        //弹窗关闭时【窗体哪个 X】
        public void OnDialogClosed()
        {
            DialogParameters keys = new DialogParameters();
            keys.Add("Info", "======INDO==========");
            RequestClose.Invoke(keys, ButtonResult.OK);
            //throw new NotImplementedException();
        }
        //弹窗弹出时触发
        public void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters.ContainsKey("zen"))
            {
                this.Title = parameters.GetValue<string>("zen");
            }
            //throw new NotImplementedException();
        }
    }
}


http://www.kler.cn/news/282948.html

相关文章:

  • RabbitMQ练习(Publish/Subscribe)
  • GPT-SoVITS-WebUI 初体验
  • C++练习题:进阶算法——动态规划
  • 面试题集锦:数据库
  • 米壳AI:做塞尔维亚跨境电商,用这个工具翻译产品主图,语言不再是难题!
  • KEYSIGHT是德 Infiniium EXR系列 示波器
  • LavaDome:一款基于ShadowDOM的DOM树安全隔离与封装工具
  • 大语言模型中,role为user、assistant、system有什么区别
  • 我主编的电子技术实验手册(18)——认识电感
  • 【数学建模】国赛论文写作教学——问题重述与分析
  • HTML实现俄罗斯方块
  • Spire.PDF for .NET【文档操作】演示:创建标记的 PDF 文档
  • 3.服务注册_服务发现
  • 低代码技术:简化应用开发,推动数字化转型
  • 树莓派4B安装golang最新版(20210520)
  • 代理IP设置白名单:让你的网络更安全高效
  • 今日算法:蓝桥杯基础题之“切面条”
  • AI学习指南深度学习篇-长短时记忆网络的调参和优化
  • uni-app的示例项目--简单的登陆页面及列表页面
  • 分享5款支持论文写作网站先稿后付的网站!
  • 构建基于I2C与UART通信的智能嵌入式机械臂抓取系统,结合OpenCV技术进行高效物体识别与动作控制的综合解决方案(代码示例)
  • CSS 中高度 100%和高度 100vh 有什么区别
  • 【STM32】定时器
  • leetcode46:全排列
  • 自动化测试员的职业前景
  • 【考研数学】如何实现高效刷题?怎么刷题?
  • 【Pytorch】生成对抗网络实战
  • 切片上传记录
  • Centos 添加双网卡 (生产环境配置记录)
  • 【区块链 + 司法存证】印记区块链电子印章 | FISCO BCOS应用案例