如何在 C++/winrt 中捕获并可能取消窗口关闭事件?我见过对此的参考:
void OnClosing(object sender, ClosingEventArgs e)
{
e.Cancel = !_allowClose;
}
(在拼写为“_e.Cancel”的引用示例中,但我假设这是一个错字)。在 C++/winrt 中,我希望上面的内容可以转换成这样的:
void OnClosing(winrt::Windows::Foundation::IInspectable const & sender, winrt::Windows::Foundation::IInspectable const & args)
{
args.Cancel(!_allowClose);
}
但是 _allowClose 没有定义,.Cancel 不是 args 的成员(必须指定 args 类型),而且最重要的是我不知道如何注册这个事件处理程序。这个应用程序中当然只有一个窗口,但我需要拦截任何通过 cntl-Q 或关闭框关闭该窗口的尝试,以便在程序中执行某些操作并可选择取消关闭。谢谢。[更新] 被告知 winrt 中没有 WindowClosing 事件,很遗憾。根据文档,有一个 WindowClosed 事件(以下是剪切和粘贴):
struct WindowClosedEventHandler : winrt::Windows::Foundation::IUnknown
{
WindowClosedEventHandler(std::nulptr_t = nullptr) noexcept {}
template <typename L> WindowClosedEventHandler(L lambda);
template <typename F> WindowClosedEventHandler(F* function);
template <typename O, typename M> WindowClosedEventHandler(O* object, M
method);
void operator()(winrt::Windows::UI::Xaml::WindowClosedEventHandler const&
sender) const;
};
但是我不知道如何注册这个活动。到目前为止,更好的是在关闭发生之前让我知道的事件,允许取消 - 但即使这么多也会有所帮助。知道窗口,唯一的窗口,正在关闭似乎至关重要。