[Prism框架实用分享]如何在主程序中合理的弹出子窗体
大家好
说起子窗体,大家都会想到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="" Height=""
Title="{Binding Title}"
x:Name="confirmationWindow" Topmost="True" WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen">
<Grid x:Name="LayoutRoot" Margin="">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions> <ContentControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="" Content="{Binding Content}"/> <Button Content="Cancel" Width="" Height="" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="">
<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="" Height="" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="">
<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="" Width="">
<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="" Height=""/>
<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的分层思想,子窗体可以随心定制,而不需要去改逻辑层的代码。具体代码以上全部提供,谢谢。
[Prism框架实用分享]如何在主程序中合理的弹出子窗体的更多相关文章
- Prism框架 如何在主程序中合理的弹出子窗体
说起子窗体,大家都会想到ChildWindow,多熟悉的一个控件.不错,Sliverlight中已经提供了子窗体的具体实现,而在WPF中却没有这么好的事情(有的第三方控件商已经提供此控件).最常见的实 ...
- [Prism框架实用分享]如何在Prism应用程序中使用日志
前言 在Prism中有关日志的命名空间: Microsoft.Practices.Prism.Logging 在Prism中,你可以使用Prism自带的Logger(比如TextLogger等),也可 ...
- bootstrap中popover.js(弹出框)使用总结+案例
bootstrap中popover.js(弹出框)使用总结+案例 *转载请注明出处: 作者:willingtolove: http://www.cnblogs.com/willingtolove/p/ ...
- 在DLL动态链接库中封装VCL的MDI子窗体
在DLL动态链接库中封装VCL的MDI子窗体不多说了,看代码就应该明白了,曾经我遇到的问题,现在放出来大家共享! 这里是工程文件的部分: 在DLL中封装MDI子窗体需要重写DLL入口函数,具体代码如下 ...
- 在没有界面的类中,实现弹出UIAlertView || 在没有界面的类中,刷新程序界面 思路
+(DisplayErrorMsg *)sharedDisplayErrorMsg { static DisplayErrorMsg *instance = nil; @synchronized(in ...
- 在IOS11中position:fixed弹出框中的input出现光标错位的问题
问题出现的背景: 在IOS11中position:fixed弹出框中的input出现光标错位的问题 解决方案 一.设计交互方面最好不要让弹窗中出现input输入框: 二.前端处理此兼容性的方案思路: ...
- firefox浏览器中 bootstrap 静态弹出框中select下拉框不能弹出(解决方案)
问题出现场景1: 在firefox浏览器中在bootstrap弹出的modal静态框中再次弹出一个静态框时 select下拉框不能弹出选项 解决方案:去掉最外层静态框的 tabindex=" ...
- 使用mui框架开发App,当input获取焦点时,键盘弹出,底部导航栏上升。
转自 https://blog.csdn.net/elementFei/article/details/72917393 感谢 问题: 使用mui框架开发App,当input获取焦点时,键盘弹出,底部 ...
- android中常用的弹出提示框
转自:http://blog.csdn.net/centralperk/article/details/7493731 我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的 ...
随机推荐
- 增量升级(省流量更新)的Android客户端实现
转载与 zhouhuiah的专栏 http://blog.csdn.net/zhouhuiah/article/details/16939937 本文在以上两篇博客的基础上再增加了异常处理,并将生成的 ...
- tomcat, jdk, eclipse, ant的安装,设置及常见问题
1.tomcat 安装: 安装版:在官方下载tomcat的安装版,根据提示一步步操作,很简单的 解压版:在官方下载tomcat的解压版,放到要安装的目录中解压版即可 同以前的找到设置环境变量的地方. ...
- SMTP邮件发送命令
第一步,远程登录smtp服务器 在命令行窗口输入 telnet smtp.163.com 25 然后回车第二步,用户登录 输入 helo 163.com 回车,这是向服务器表明你的用户身份250 OK ...
- (旧)子数涵数·PS——替换颜色
一,老规矩,下载素材(老样子,还是美美的少女) 二.打开PS,并打开素材. 三.复制一个图层,快捷键Ctrl+J(好习惯) 四.使用"替换颜色"命令,弹出对话框. 五.选择人物衣服 ...
- 设置Eclipse的字体风格方式
1:Eclipse版本号:3.7.0(汉化版) 假设eclipse已经打开 窗口--->首选项--->常规--->外观--->颜色和字体--->基本--->文字字体 ...
- 洛谷P2737 [USACO4.1]麦香牛块Beef McNuggets
P2737 [USACO4.1]麦香牛块Beef McNuggets 13通过 21提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交 讨论 题解 最新讨论 暂时没有讨论 题目描 ...
- c#中如何得到百分比数值
//此方法得到的百分比后小数太多,不行double percent=Convert.ToDouble(2)/Convert.ToDouble(34);string result=(percent*10 ...
- CSS垂直水平居中方法整理
CSS定位中常常用到垂直居中,比如覆盖层上的弹框. 兼容性比较好的方法: <!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transition ...
- JS与JQ倒计时的写法
页面需要制作一个倒计时的功能:然后度娘了一遍,找到两种写法,原生JS与JQ 的,经过测试原生JS在IE可能会有不刷新的现象所以结合了一个大神的JQ写法修改好了一个. 原生JS写法: HTML: < ...
- 【MySQL】MySQL回滚工具
1.mysqlbinlog把事务从binlog中导出 2.从导出的binlog中找到要回滚的事务,去掉第一个DML语句前和最后一个DML语句后与DML无关的binlog信息 3.在目录中新建一个tab ...