大家好

说起子窗体,大家都会想到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框架实用分享]如何在主程序中合理的弹出子窗体的更多相关文章

  1. Prism框架 如何在主程序中合理的弹出子窗体

    说起子窗体,大家都会想到ChildWindow,多熟悉的一个控件.不错,Sliverlight中已经提供了子窗体的具体实现,而在WPF中却没有这么好的事情(有的第三方控件商已经提供此控件).最常见的实 ...

  2. [Prism框架实用分享]如何在Prism应用程序中使用日志

    前言 在Prism中有关日志的命名空间: Microsoft.Practices.Prism.Logging 在Prism中,你可以使用Prism自带的Logger(比如TextLogger等),也可 ...

  3. bootstrap中popover.js(弹出框)使用总结+案例

    bootstrap中popover.js(弹出框)使用总结+案例 *转载请注明出处: 作者:willingtolove: http://www.cnblogs.com/willingtolove/p/ ...

  4. 在DLL动态链接库中封装VCL的MDI子窗体

    在DLL动态链接库中封装VCL的MDI子窗体不多说了,看代码就应该明白了,曾经我遇到的问题,现在放出来大家共享! 这里是工程文件的部分: 在DLL中封装MDI子窗体需要重写DLL入口函数,具体代码如下 ...

  5. 在没有界面的类中,实现弹出UIAlertView || 在没有界面的类中,刷新程序界面 思路

    +(DisplayErrorMsg *)sharedDisplayErrorMsg { static DisplayErrorMsg *instance = nil; @synchronized(in ...

  6. 在IOS11中position:fixed弹出框中的input出现光标错位的问题

    问题出现的背景: 在IOS11中position:fixed弹出框中的input出现光标错位的问题 解决方案 一.设计交互方面最好不要让弹窗中出现input输入框: 二.前端处理此兼容性的方案思路: ...

  7. firefox浏览器中 bootstrap 静态弹出框中select下拉框不能弹出(解决方案)

    问题出现场景1: 在firefox浏览器中在bootstrap弹出的modal静态框中再次弹出一个静态框时 select下拉框不能弹出选项 解决方案:去掉最外层静态框的 tabindex=" ...

  8. 使用mui框架开发App,当input获取焦点时,键盘弹出,底部导航栏上升。

    转自 https://blog.csdn.net/elementFei/article/details/72917393 感谢 问题: 使用mui框架开发App,当input获取焦点时,键盘弹出,底部 ...

  9. android中常用的弹出提示框

    转自:http://blog.csdn.net/centralperk/article/details/7493731 我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的 ...

随机推荐

  1. HDU 4390 Number Sequence 容斥原理

    Number Sequence Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. Cocos2d-x3.2 使用物理引擎进行碰撞检测[转]

    通常在游戏简单逻辑判断和模拟真实的物理世界时,我们只需要在定时器中判断游戏中各个精灵的条件是否满足判断条件就可以了.例如,在飞机大战中,判断我方子弹和敌机是否发生碰撞一般在定时器中通过敌机所在位置的矩 ...

  3. 一道 JavaScript 面试题

    有一道 JavaScript 面试题. f = function () { return true; }; g = function () { return false; }; (function() ...

  4. [原创] IIS7下顶级域名301跳转到WWW域名

    百度搜索了众多方法,居然没有一个全面的IIS7下301域名跳转能用的教程,最终自己研究出了个可以用的供大家参考.1.绑定域名01ruodian.cn www.01ruodian.cn到空间: 2.在I ...

  5. 剑指Offer:面试题16——反转链表(java实现)

    问题描述 定义一个函数,输入一个链表的头结点,反转该链表并输出反转后的链表的头结点.链表结点如下: public class ListNode { int val; ListNode next = n ...

  6. Gradle用户指南(章9:Groovy快速入门)

    Gradle用户指南(章9:Groovy快速入门) 你可以使用groovy插件来构建groovy项目.这个插件继承了java插件的功能,且扩展了groovy编译.你的项目可以包含groovy代码.ja ...

  7. Oozie JMS通知消息实现--根据作业ID来过滤消息

    一,介绍 本文使用Oozie的消息通知功能,并根据JMS规范中的消息选择器(Selector)实现 根据作业的ID来过滤消息. 首先搭建好JMS Provider(ActiveMQ) ,并进行相关配置 ...

  8. 洛谷P1461 海明码 Hamming Codes

    P1461 海明码 Hamming Codes 98通过 120提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 给出 N,B 和 ...

  9. linx 实用操作命令一

    如果apache安装成为linux的服务的话,可以用以下命令操作:service httpd start 启动service httpd restart 重新启动service httpd stop ...

  10. .net HttpClient类(System.Net.Http) c#并发使用测试、压测

    .net HttpClient类(System.Net.Http) c#并发使用测试.压测