【问题标题】:Event at app shutdown Windows Phone 8.1应用程序关闭时的事件 Windows Phone 8.1
【发布时间】:2014-10-07 15:50:44
【问题描述】:

我正在使用相机为我的 Windows Phone 创建我的第一个应用程序。在关机时,我想调用一个 dispose-method 从MediaCapture object 释放资源。但是我找不到在应用程序关闭时触发的事件。

有谁知道我如何在关机时处理这个对象?因为现在关闭应用程序会使我的手机死机。

【问题讨论】:

  • 您的目标是 Silverlight 还是 Runtime
  • @Romasz 哈哈,其实我不确定...如何检查我的目标是什么?
  • 例如在 Solution Explorer 窗口中,您可以在项目名称旁边看到 Windows Phone 8.1Windows Phone Silverlight 8.1 .此外,您的MainPageWinRT 中的类型为Page,在Silverlight 中为PhoneApplicationPage。当您选择新项目时,您可以选择 Blank AppBlank App (Silverlight) - 第二个应该在下面的模板中。

标签: c# wpf windows-phone-8.1


【解决方案1】:

在你的 app.xaml.cs 中你通常会得到这个类

    public sealed partial class App : Application

当你创建项目时,你已经有了两个有趣的方法

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
   // some code here
   // will run when app launch
}

还有这个

/// <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)
{
   // some code here
}

因此,您可以在解释该功能的摘要中阅读,当用户暂停应用程序时调用它,但您不知道应用程序是否会终止或稍后恢复,我认为您没有区分的方法。

所以我建议在 OnSuspending 函数中处理你的资源

这适用于 Windows Phone 8.1 和 Windows 8.1 Metro 风格的应用程序

如果你想在 WPF 项目中这样做,你实际上有一个

OnExit(ExitEventArgs e)

在此处查看 Msdn 文档(仅适用于 WPF)

Msdn OnExit documentation page

【讨论】:

  • 我有一种预感,你必须使用这种方法,但我想确定一下。感谢您的帮助,我的手机不再冻结,因为我现在可以正确处理了。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多