简体   繁体   English

应用程序关闭Windows Phone 8.1时发生的事件

[英]Event at app shutdown Windows Phone 8.1

I'm creating my first application for my Windows Phone using the camera. 我正在使用相机为Windows Phone创建我的第一个应用程序。 And at shutdown I want to call a dispose-method to release resources from a MediaCapture object . 在关闭时,我想调用一个dispose方法从MediaCapture object释放资源。 But I cannot find an event which triggers when the application shuts down. 但是我找不到一个在应用程序关闭时触发的事件。

Does anyone know how I can dispose this object at shutdown? 有谁知道我如何在关机时处置该对象? Cause when closing the application now makes my phone freeze. 现在,当关闭应用程序时,原因导致手机冻结。

In your app.xaml.cs you normally got this class 在您的app.xaml.cs中,您通常会获得此类

    public sealed partial class App : Application

Inside you got two interresting methods already there when you create the project 在创建项目时,内部已经有两种有趣的方法

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

And this one 还有这个

/// <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
}

So as you can read in the summary that explains the function, it is called when user suspend the application, but you don't know if the application will terminate or be resumed later, and I think you don't have a way to differentiate. 因此,如您在解释功能的摘要中所读,当用户挂起应用程序时会调用该函数,但是您不知道该应用程序将终止还是稍后恢复,因此我认为您没有办法。

So I would suggest dispose your ressources inside the OnSuspending function 因此,我建议将您的资源放置在OnSuspending函数中

That is for Windows Phone 8.1 and Windows 8.1 metro style application 那是针对Windows Phone 8.1和Windows 8.1 Metro风格的应用程序

If you want to do that in a WPF project, you actually got an 如果要在WPF项目中执行此操作,实际上

OnExit(ExitEventArgs e)

see Msdn documentation here (for WPF only) 请参阅此处的Msdn文档(仅适用于WPF)

Msdn OnExit documentation page MSdn OnExit文档页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM