大家好

说起子窗体,大家都会想到ChildWindow,多熟悉的一个控件。不错,Sliverlight中已经提供了子窗体的具体实现,而在WPF中却没有这么好的事情(有的第三方控件商已经提供此控件)。最常见的实现方法就是在ViewModel中,直接New ChildWindow,然后直接Show。这样的方法也达到的要求。但是它不符合MVVM分层思想,再就是代码不美观,难以维护,今天我就给大家介绍一种美观又实用的方法。

原理

通过Prism中提供的InteractionRequestTrigger事件触发器,实现点击按钮或者用户的某种操作弹出对话框的效果。另外,不要忘了引用此命名空间:

using Microsoft.Practices.Prism.Interactivity.InteractionRequest;

创建ChildWindow窗体

<Window x:Class="ChildWindowDemo.ChildWindow.ChildWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"  xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"Width="300" Height="150" Title="{Binding Title}"x:Name="confirmationWindow" Topmost="True" WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen"><Grid x:Name="LayoutRoot" Margin="2"><Grid.RowDefinitions><RowDefinition /><RowDefinition Height="Auto" /></Grid.RowDefinitions><ContentControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" Content="{Binding Content}"/><Button Content="Cancel" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1"><i:Interaction.Triggers><i:EventTrigger EventName="Click"><ei:CallMethodAction TargetObject="{Binding ElementName=confirmationWindow}" MethodName="Close"/></i:EventTrigger></i:Interaction.Triggers></Button><Button Content="OK" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1"><i:Interaction.Triggers><i:EventTrigger EventName="Click"><ei:ChangePropertyAction PropertyName="Confirmed" TargetObject="{Binding}" Value="True"/><ei:CallMethodAction TargetObject="{Binding ElementName=confirmationWindow}" MethodName="Close"/></i:EventTrigger></i:Interaction.Triggers></Button></Grid>
</Window>

创建ChildWindow的基类

新建类:ChildWindowActionBase 并从TriggerAction<T>派生,代码如下:

    public class ChildWindowActionBase : TriggerAction<FrameworkElement>{protected override void Invoke(object parameter){var arg = parameter as InteractionRequestedEventArgs;if (arg == null)return;var windows = this.GetChildWindow(arg.Context);var callback = arg.Callback;EventHandler handler = null;handler =(o, e) =>{windows.Closed -= handler;callback();};windows.Closed += handler;windows.ShowDialog();}Window GetChildWindow(Notification notification){var childWindow = this.CreateDefaultWindow(notification);childWindow.DataContext = notification;return childWindow;}Window CreateDefaultWindow(Notification notification){return (Window)new ChildWindow.ChildWindow();}}   

到此子窗体已经完成

如何调用

主程序界面代码如下:

<Window x:Class="ChildWindowDemo.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:prism="http://www.codeplex.com/prism"xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"xmlns:local="clr-namespace:ChildWindowDemo"Title="MainWindow" Height="200" Width="300"><i:Interaction.Triggers><prism:InteractionRequestTrigger SourceObject="{Binding ConfirmationRequest, Mode=OneWay}"><local:ChildWindowActionBase/></prism:InteractionRequestTrigger></i:Interaction.Triggers><Grid><Button Command="{Binding RaiseConfirmation}" Content="Click Me !" HorizontalAlignment="Left" Margin="29,31,0,0" VerticalAlignment="Top" Width="217" Height="55"/><TextBlock HorizontalAlignment="Left" Margin="29,106,0,0" TextWrapping="Wrap" Text="{Binding ConfirmationResult}" VerticalAlignment="Top"/></Grid>
</Window>

对之对应的ViewModel:

    public class MainWindowViewModel : NotificationObject{public MainWindowViewModel(){this.RaiseConfirmation = new DelegateCommand(this.OnRaiseConfirmation);this.ConfirmationRequest = new InteractionRequest<Confirmation>();}public InteractionRequest<Confirmation> ConfirmationRequest { get; private set; }public DelegateCommand RaiseConfirmation { get; private set; }private string result;public string ConfirmationResult{get { return result; }set{result = value;this.RaisePropertyChanged(() => this.ConfirmationResult);}}private void OnRaiseConfirmation(){this.ConfirmationRequest.Raise(new Confirmation { Content = "是否确认", Title = "子窗体" },(cb) => { ConfirmationResult = cb.Confirmed ? "确认" : "取消"; });}      }

总结

这样的写法比较符合MVVM的分层思想,子窗体可以随心定制,而不需要去改逻辑层的代码。具体代码以上全部提供,谢谢。

转载于:https://www.cnblogs.com/sunthx/p/3539900.html

[Prism框架实用分享]如何在主程序中合理的弹出子窗体相关推荐

  1. js layui 弹出子窗体_Layui中JS实现弹出层的应用

    点我 //弹出一个页面层 var oBtn = document.getElementById('tab_1'); oBtn.onclick = function (){ layer.open({ t ...

  2. java鼠标右击出现选择窗口_java菜单代码 java中鼠标右击弹出菜单怎样实现

    帮忙给一个java菜单栏例子的源代码 给你个小例子,已经添加注释了.自己运行下看看效果,满意的话记得结贴子. import java.awt.BorderLayout; import java.awt ...

  3. 如何在WordPress中创建优惠券弹出窗口(逐步)

    Do you want to create a coupon popup in WordPress? Coupon popups are a great way to convert your web ...

  4. iOS下Html页面中input获取焦点弹出键盘时挡住input解决方案

    iOS下Html页面中input获取焦点弹出键盘时挡住input解决方案 参考文章: (1)iOS下Html页面中input获取焦点弹出键盘时挡住input解决方案 (2)https://www.cn ...

  5. iOS下Html页面中input获取焦点弹出键盘时挡住input解决方案—scrollIntoView()

    iOS下Html页面中input获取焦点弹出键盘时挡住input解决方案-scrollIntoView() 参考文章: (1)iOS下Html页面中input获取焦点弹出键盘时挡住input解决方案- ...

  6. Angular2项目中浏览器拦截弹出窗口的解决方法

    Angular2项目中浏览器拦截弹出窗口的解决方法 为什么把项目是Angular2的放到了前边? 因为正常也页面网上已经很多解决方案.请自行百度或Google. 现象:当window.open为用户触 ...

  7. ASP.NET中WEB上弹出消息框的N种方法(为了以后方便,转了很多网友的文章!希望不会介意)...

    ASP.NET中WEB上弹出消息框的N种方法 第一个确定之后跳转到另一页面,第二个确定之后返回前一页 Response.Write("<script langage='javascri ...

  8. Android中EditText优先弹出数字输入法

    Android Android中EditText优先弹出数字输入法 项目中一个EditText输入数字的频率较高,但是汉字和英文的输入也偶有需要,直接在xml文件将inputType设置为number ...

  9. 解决微信小程序IOS中使用picker弹出内容和手机软键盘重叠的问题

    解决微信小程序IOS中使用picker弹出内容和手机软键盘重叠的问题 项目需求: 一个信息提交页面:有input输入框,有picker选择器 遇到的问题: 点击input输入框时,手机自动弹出键盘,但 ...

最新文章

  1. 要让OKR真正”OK“,这三点一定要注意!
  2. 每日一皮:产品经理的黑化,你听过几个?
  3. PhpForm表单验证
  4. [Jsp] JSP和Servlet页面间的参数的传递和接收
  5. Spring入门(一)之简介
  6. STL源码剖析 第七章 仿函数(函数对象)
  7. SpringCloud集成lombox(eclipes工具)
  8. 带你读AI论文:SDMG-R结构化提取—无限版式小票场景应用
  9. python网络爬虫学习笔记(八):XPath的使用
  10. 驱动人生win7系统如何升级win10一键装机图文教程
  11. ps6人脸识别液化工具在哪_Photoshop教学:人脸识别液化功能介绍
  12. iEx.ec——云计算业务的区块链革命
  13. 因为制作爬虫程序,我收到了警告
  14. 如何联系海外网红?4个方法助你提高海外网红回复率
  15. syslinux linux 启动盘,syslinux启动盘制作
  16. 简单好用微服务套件AnnoViper DashBoard全新版来啦
  17. 【数据分析案例】从微信接龙中随机选择k名人员出席
  18. Google谷歌中国总裁李开复将离职创业
  19. Linux下的SMB服务(samba服务器)
  20. windows下SourceTree的安装位置,用于创建快捷方式到桌面

热门文章

  1. scrapy pipelines.py
  2. TensorFlow tf.data.Dataset
  3. 1.12 改善你的模型的表现
  4. git 应用 cherry-pick
  5. 微型计算机天逸510s光驱,主机届的小钢炮,性能最强NAS——天逸510S Mini
  6. XEON® Scalable-如何为虚拟化挑选合适的CPU
  7. Java基础学习总结(142)——以正确的姿势使用Java 8 Optional
  8. JAVA程序设计第十版第七章_java程序设计第七章答案
  9. java改变线程堆栈大小,在运行时更新java线程的堆栈大小
  10. java 动态树_使用dtree构建动态树型菜单