WPF 命令基础结构中是否有用于对话框的关闭命令?

发布于 2024-09-06 12:07:27 字数 172 浏览 1 评论 0原文

我应该使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅气称霸 2024-09-13 12:07:27

以下是 WPF 使用 ApplicationCommand.Close 的方式:

  1. WPF 本身没有内置的 ApplicationCommands.Close 命令处理程序,但
  2. WPF 每当收到 Win32 消息时都会执行 ApplicationCommand.Close RoutedCommand WM_APPCOMMANDlParam=APPCOMMAND_CLOSE。这意味着如果任何 Win32 应用程序向您发送 APPCOMMAND_CLOSE,您的 ApplicationCommand.Close 处理程序将被调用。

APPCOMMAND_CLOSE文档 code> 给出了这个定义:

关闭窗口(而不是应用程序)。

我认为 WPF 应用程序应该以相同的方式处理 ApplicationCommand.Close,并且“窗口”将包括对话框(在 Win32 的世界中通常如此)。

为什么你关心 Win32 文档的内容?它在三种情况下可能很重要:

  1. 辅助功能场景
  2. 具有“关闭窗口”键的键盘
  3. 当用户配置了鼠标按钮组合来发送“关闭窗口”命令

因此,回答您的问题:不,ApplicationCommand。 Close 保留用于关闭应用程序。其目的是关闭窗口,包括对话框窗口。因此无需创建单独的命令来关闭对话框。

许多应用程序只是使用 App.xaml 中的样式在 Window 类上设置 CommandBinding,并在处理程序中通过调用 ((Window)sender).Close() 结束。这是一个简单而优雅的解决方案。

Here is how WPF uses ApplicationCommand.Close:

  1. WPF itself has no built in command handler for ApplicationCommands.Close, but
  2. WPF executes the ApplicationCommand.Close RoutedCommand whenever it receives the Win32 message WM_APPCOMMAND with lParam=APPCOMMAND_CLOSE. This means that if any Win32 application sends you an APPCOMMAND_CLOSE, your ApplicationCommand.Close handler will be called.

The documentation for APPCOMMAND_CLOSE gives this definition:

Close the window (not the application).

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:

  1. Accessibilty scenarios
  2. Keyboards that have a "close window" key
  3. When the user has configured a combination of mouse buttons to send the "close window" command

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.

烦人精 2024-09-13 12:07:27

如果您只是想绑定按钮单击以关闭对话框,则可以在按钮上设置 IsCancel 属性 按照文档中的建议

<Button IsCancel="True">Cancel</Button>

当您单击它时,它将关闭对话框并显示 <代码>假结果。如果您想以 true 结果关闭,一种非常简单的方法是添加点击处理程序并编写两 (2) 行隐藏代码。您的 XAML 是:

<Button IsDefault="True" Click="acceptButton_Click">OK</Button>

隐藏代码:

void acceptButton_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
}

这将关闭对话框并返回 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:

<Button IsCancel="True">Cancel</Button>

When you click that, it will close the dialog with a false result. If you want to close with a true result, one very simple way is adding a click handler and writing two (2) lines of code-behind. Your XAML is:

<Button IsDefault="True" Click="acceptButton_Click">OK</Button>

And the code-behind:

void acceptButton_Click(object sender, RoutedEventArgs e)
{
    this.DialogResult = true;
}

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.

月隐月明月朦胧 2024-09-13 12:07:27

关闭应用程序与关闭对话框不同。

有一个用于关闭对话框的内置命令。您创建的任何 Window 类的顶部都有一个红色的大“X”。当您点击“X”时,您会生成一个“DialogCancelCommand”,然后由 Window 类内的“OnDialogCancel”事件处理程序处理。然而,由于只有雷蒙德内部已知的原因,“DialogCancelCommand”是一个内部 RoutedEvent。
我不能代表其他工程师,但我从反射代码中复制了“DialogCancelCommand”,以便我可以以与内部命令执行相同操作的方式相同的方式关闭对话框。

    /// <summary>
    /// Cancels a dialog.
    /// </summary>
    public static readonly RoutedCommand DialogCancelCommand = new RoutedCommand("DialogCancel", typeof(CoreCommands));

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.

    /// <summary>
    /// Cancels a dialog.
    /// </summary>
    public static readonly RoutedCommand DialogCancelCommand = new RoutedCommand("DialogCancel", typeof(CoreCommands));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文