大家好

说起子窗体,大家都会想到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. IEnumerable和List有什么区别?

    如下.IList接口可以使用更多的方法.比如你看一个集合是否包含相应实体, IEnumerable不行,而 IList里有Contains,相应的实现了IList的可以添加,删除相应实体.而IEnum ...

  2. 翻译:Knockout 快速上手 - 3: knockoutJS 快速上手

    许多时候,学会一种技术的有效方式就是使用它解决实际中的问题.在这一节,我们将学习使用 Knockout 来创建一个常见的应用,库存管理应用. 应用概览 在创建我们的应用之前,我们需要一个公司,来理解应 ...

  3. linux下启动dbca或netmgr类的图形界面报错解决

    linux下启动dbca或netmgr类的图形界面报错解决    Xlib: connection to ":0.0" refused by server Xlib: No pro ...

  4. Laxcus大数据管理系统2.0(14)- 后记

    后记 Laxcus最早源于一个失败的搜索引擎项目,项目最后虽然终止了,但是项目中的部分技术,包括FIXP协议.Diffuse/Converge算法.以及很多新的数据处理理念却得以保留下来,这些成为后来 ...

  5. ORA-01461: 仅可以为插入 LONG 列的 LONG 值赋值”解决办法

    ORA-01461: 仅可以为插入 LONG 列的 LONG 值赋值”解决办法 今天遇到的一个问题: 直接修改数据表中的某些字段数据内容时报错: ORA-01461: 仅可以为插入 LONG 列的 L ...

  6. cygwin中运行命令提示command not found的解决方法

    在cygwin下运行ls等linux常见命令时出现“command not found”的提示,原因是环境变量没有配置好,因此只要将环境变量配置正确,即可正常使用.举例说明,cygwin安装在C盘根目 ...

  7. Xcode去除某种类型的警告

      首先来看下当有警告时,怎么找到警告类型,在某条警告上,右键—>Reveal in Log     下面 [ ] 中间就是警告信息     去除警告信息的几种方式:   一.使用编译器提供的宏 ...

  8. MSP430F149学习之路——AD

    代码一:Timer_A触发转换 #include <msp430x14x.h> void main() { WDTCTL = WDTPW + WDTHOLD; P6SEL |= BIT0; ...

  9. 智能指针(三):unique_ptr使用简介

    我们知道auto_ptr通过复制构造或者通过=赋值后,原来的auto_ptr对象就报废了.所有权转移到新的对象中去了.而通过shared_ptr可以让多个智能指针对象同时拥有某一块内存的访问权.但假如 ...

  10. js的二元三元操作符

    二元 if ( a == b) { alert(a) } // (a == b) && alert(a) if ( a != b) { alert(a) } // (a == b) | ...