WPF 命令基础结构中是否有用于对话框的关闭命令?
我应该使用 ApplicationCommands.Close
来关闭模式对话框,还是该命令被视为保留用于关闭应用程序?如果是后者,人们是为每个Dialog
框创建Close
命令还是为所有模式对话框创建一个Close
命令?
Should I use ApplicationCommands.Close
for closing modal dialog boxes or is that command considered reserved for closing the application? If it is the latter, do folks create Close
commands for each Dialog
box or just a single Close
command for all their modal dialog boxes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是 WPF 使用 ApplicationCommand.Close 的方式:
ApplicationCommands.Close
命令处理程序,但ApplicationCommand.Close
RoutedCommandWM_APPCOMMAND
与lParam=APPCOMMAND_CLOSE
。这意味着如果任何 Win32 应用程序向您发送APPCOMMAND_CLOSE
,您的ApplicationCommand.Close
处理程序将被调用。APPCOMMAND_CLOSE文档 code> 给出了这个定义:
我认为 WPF 应用程序应该以相同的方式处理
ApplicationCommand.Close
,并且“窗口”将包括对话框(在 Win32 的世界中通常如此)。为什么你关心 Win32 文档的内容?它在三种情况下可能很重要:
因此,回答您的问题:不,ApplicationCommand。 Close 不保留用于关闭应用程序。其目的是关闭窗口,包括对话框窗口。因此无需创建单独的命令来关闭对话框。
许多应用程序只是使用 App.xaml 中的样式在 Window 类上设置 CommandBinding,并在处理程序中通过调用
((Window)sender).Close()
结束。这是一个简单而优雅的解决方案。Here is how WPF uses ApplicationCommand.Close:
ApplicationCommands.Close
, butApplicationCommand.Close
RoutedCommand whenever it receives the Win32 messageWM_APPCOMMAND
withlParam=APPCOMMAND_CLOSE
. This means that if any Win32 application sends you anAPPCOMMAND_CLOSE
, yourApplicationCommand.Close
handler will be called.The documentation for
APPCOMMAND_CLOSE
gives this definition:I would assume WPF applications should treat
ApplicationCommand.Close
the same way, and that "the window" would include dialog boxes (it generally does in the world of Win32).Why do you care about what the Win32 documentation says? It might be important in three situations:
So to answer your question: No,
ApplicationCommand.Close
is not reserved for closing the application. Its purpose is to close the window, including a dialog box window. So there is no need to create separate commands to close dialog boxes.Many applications simply use a style in App.xaml to set a CommandBinding on thw Window class, and in the handler they end by calling
((Window)sender).Close()
. This is a simple and elegant solution.如果您只是想绑定按钮单击以关闭对话框,则可以在按钮上设置
IsCancel
属性 按照文档中的建议:当您单击它时,它将关闭对话框并显示 <代码>假结果。如果您想以
true
结果关闭,一种非常简单的方法是添加点击处理程序并编写两 (2) 行隐藏代码。您的 XAML 是:隐藏代码:
这将关闭对话框并返回 true 结果。
由于这是查看代码(关闭窗口),因此这种隐藏代码通常不会给我严格的 MVVM 架构带来任何压力。 YDMV。
If you're just looking to bind a button click to close a dialog, you can set the
IsCancel
property on the button as recommended in the docs:When you click that, it will close the dialog with a
false
result. If you want to close with atrue
result, one very simple way is adding a click handler and writing two (2) lines of code-behind. Your XAML is:And the code-behind:
This will close the dialog and return a
true
result.Since this is view code (close a window), this code-behind doesn't typically cause me any duress in my strict MVVM architectures. YDMV.
关闭应用程序与关闭对话框不同。
有一个用于关闭对话框的内置命令。您创建的任何 Window 类的顶部都有一个红色的大“X”。当您点击“X”时,您会生成一个“DialogCancelCommand”,然后由 Window 类内的“OnDialogCancel”事件处理程序处理。然而,由于只有雷蒙德内部已知的原因,“DialogCancelCommand”是一个内部 RoutedEvent。
我不能代表其他工程师,但我从反射代码中复制了“DialogCancelCommand”,以便我可以以与内部命令执行相同操作的方式相同的方式关闭对话框。
Closing an application is different than closing a dialog.
There is a built in command for dismissing dialogs. There is a big red 'X' at the top of any Window class that you create. When you hit that 'X' you generate a 'DialogCancelCommand' which is then handled by the 'OnDialogCancel' event handler inside the Window class. However, for reasons that are known only inside Redmond, the 'DialogCancelCommand' is an internal RoutedEvent.
I can't speak for other engineers, but I have reproduced the 'DialogCancelCommand' from the Reflected code so that I can close dialog boxes identically to the way the internal commands perform the same operation.