执行ApplicationCommands.Close时实际发生的情况 [英] What actually happens when ApplicationCommands.Close is executed

查看:31
本文介绍了执行ApplicationCommands.Close时实际发生的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想标题中的问题很清楚.当我打电话

I guess the question in the title is clear enough.What happens when i call

ApplicationCommands.Close.Execute(null,null)

来自我的viewmodel类.

from my viewmodel class.

我有一个显示用户控件的模型对话框.我有一个绑定到viewmodel中执行的用户控件中的按钮的命令.我想在命令执行后关闭对话框.可以使用上面的命令来完成到执行事件的末尾.

I have a modeldialog which shows a usercontrol.I have a commandbinding to a button in the usercontrol which is executed in the viewmodel.I want to close the dialog after the command is executed.It can be done by using the above command to the end of the executed event.

但是我想知道这是否是正确的方法.这会导致任何不良后果吗?

But i am wondering if this is the right approach.Will this cause any undesired effect ?

推荐答案

不幸的是,这行不通.

ApplicationCommands.Close是一个RoutedUiEvent,它需要一个Commandtarget(任何IInputelement).它使用此命令目标来引发routedevent.

ApplicationCommands.Close is a RoutedUiEvent which requires a Commandtarget (any IInputelement). It uses this command target to raise a routedevent.

然后您可以使用命令绑定来处理此routedevent.

You can then handle this routedevent via using a commandbinding.

在ViewModel中,您可以绑定ApplicationCommands.关闭以关闭按钮,然后在窗口中处理路由的关闭事件.

From the ViewModel you could bind ApplicationCommands.Close to Close Button and then handle the routed close event in your window.

但是,由于没有CommandTarget(第二个null),在执行了另一个命令后,您无法在ViewModel中调用 ApplicationCommands.Close.Execute(null,null)

However you can't call ApplicationCommands.Close.Execute(null,null) in your ViewModel after another command is executed as you have no CommandTarget (the second null)

还有其他类似这样的主题应该如何ViewModel是否关闭表单?其中详细介绍了实现此目的的各种方法.

There are other threads like this one How should the ViewModel close the form? which detail various methods to acheive this.

http://adammills.wordpress.com/2011/02/16/mvvm-uses-for-applicationcommands-close/

这篇关于执行ApplicationCommands.Close时实际发生的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆