恢复和暂停 WinRT 相机应用程序

Resuming and suspendig WinRT Camera App

我有一个包含三个页面的应用程序:MainPagePage2Page3。在 MainPage 我有我的相机,在 Page2 我有来自 MainPage 的历史扫描,我的 Page3 有一些关于应用程序的信息。我的问题是何时暂停以及何时恢复。它可以正常暂停,但是当我在 Visual Studio LifeStyle Events 按下恢复按钮时,出现以下错误 A remote operation is taken longer then expected。如您所见,我的应用程序需要很长时间才能恢复。

在我的 App.xaml.cs 我有这个 OnSuspending 方法:

public App()
{
    this.InitializeComponent();
    this.Suspending += this.OnSuspending;
}

private void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
     deferral.Complete();
}

在我的 MainPage 中,我有这个恢复方法:

public MainPage()
{
    this.InitializeComponent();
    Application.Current.Resuming += App_resuming;
}

async void App_resuming(object sender, object 
{
    if (Frame.Content == this)
    await InitializeCamera();
}

我想当我暂停应用程序时我必须处理我的相机,所以我也在 MainPage 创建了一个暂停方法,我在那里处理我的相机

void App_suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
   _mediaCapture.Dispose();
}

所以,只是重新表述我的问题:我在做什么是对的?我错过了什么?谢谢。

当我没有将相机对象置于暂停状态时,我可以重现你的问题,你说你已经尝试过但仍然需要更长的时间。我可以建议您检查相机对象是否真的得到处理。为确保您可以在 OnSuspend 中添加以下代码行:

await cameraCapture.StopPreview();
cameraCapture.Dispose();
cameraCapture = null;

并在初始化相机之前的恢复中,再次创建对象。