【问题标题】:Accessing the close button on a window UWP C#访问窗口 UWP C# 上的关闭按钮
【发布时间】:2017-07-13 21:43:21
【问题描述】:

我进行了一些研究,但没有发现太多对 UWP 有用的东西。当在窗口上点击“X”时,我需要在应用程序实际关闭之前调用一个函数。我似乎找不到任何处理 MVVM Cross 和 UWP 的资源。似乎有办法,但只是网上缺乏资源。我真的只是希望被指出正确的方向。

【问题讨论】:

  • 听起来你想劫持回调?

标签: c# uwp mvvmcross


【解决方案1】:

当您创建一个空白应用程序并转到 App.xaml.cs 时,您应该会在文件末尾看到一个事件 OnSuspending

见下文

/// <summary>
/// Invoked when application execution is being suspended.  Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">The source of the suspend request.</param>
/// <param name="e">Details about the suspend request.</param>
private void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
    //TODO: Save application state and stop any background activity
    deferral.Complete();
}

这就是你要找的。​​p>

【讨论】:

  • 只是想补充一点,在系统结束之前,您有最多 5 秒的时间来完成您的工作。
【解决方案2】:

您可以使用 confirmAppClose 功能覆盖关闭按钮。但是,要使用此功能,您需要将其添加到您的 appxmanifest 中,并要求 Microsoft 获得认证。

Override Close Box on Window 10 Universal Apps UWP

【讨论】:

    【解决方案3】:

    我不确定你所说的处理 MVVM Cross 是什么意思。要在应用程序关闭/停用之前处理或调用函数,您可以调用以下事件:

    Window.Current.Activated += (s, e) =>
    {
        //closed, activated, deactivate
    };
    Window.Current.Closed += (ss, ee) =>
    {
        //closed
    };
    

    【讨论】:

    • 这行不通。我认为您无法在 窗口上监视 Closed 事件。
    • 我测试过,对我有用,不知道你说的无法监控关闭事件是什么意思?
    猜你喜欢
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多