【问题标题】:(Windows phone 10) Handle Application state(Windows phone 10) 处理应用程序状态
【发布时间】:2016-09-08 05:06:58
【问题描述】:

我正在 windows phone 10 中开发应用程序

出于某种原因,我必须处理应用程序状态(进入后台,进入前台)。我在 App.xaml.cs 处理了事件挂起和恢复,但它不起作用,未达到 OnSuspending 和 OnResuming。请帮我检查我的源代码并告诉我如何处理这些事件。

这是我的代码:

public App()
    {
        Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
            Microsoft.ApplicationInsights.WindowsCollectors.Metadata |
            Microsoft.ApplicationInsights.WindowsCollectors.Session);
        this.InitializeComponent();
        this.Suspending += OnSuspending;
        Application.Current.Suspending += new SuspendingEventHandler(OnSuspending);
        Application.Current.Resuming += new EventHandler<Object>(OnResuming);

    }

    private void OnSuspending(Object sender, Windows.ApplicationModel.SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        //TODO: Save application state and stop any background activity
        deferral.Complete();


    }
    private void OnResuming(object sender, object e)
    {
        // do some thing
    }

【问题讨论】:

  • 如果您尝试调试它,通常不会触发这些事件 - 您必须使用 生命周期选项卡 - see this question 以获得更多帮助。

标签: c# win-universal-app resume suspend application-state


【解决方案1】:

您订阅了两次暂停事件

 this.Suspending += OnSuspending;
 Application.Current.Suspending += new SuspendingEventHandler(OnSuspending);

最好离开经典

 this.Suspending += OnSuspending;
 this.Resuming += App_Resuming;

同样的方式添加恢复事件

 private void App_Resuming(object sender, object e)
    {
       // TODO
    }

您是否调试的是挂起/恢复事件,如本文所述:
How to trigger suspend, resume, and background events for Windows Store apps in Visual Studio

【讨论】:

    【解决方案2】:

    您应该使用 Visual Studio 2015 的 Lifecycle Events 下拉菜单。它允许您在 SuspendResumeSuspend 和 Shutdown 状态之间进行选择。

    当您在调试中运行应用时,它永远不会自行进入挂起状态。

    这里有一些文档:https://blogs.windows.com/buildingapps/2016/04/28/the-lifecycle-of-a-uwp-app/

    【讨论】:

      猜你喜欢
      • 2016-04-15
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2016-02-17
      相关资源
      最近更新 更多