这适用于从文件选取器示例创建的 Windows 通用应用程序。该示例的基本代码包括ContinuationManager
Windows Phone 项目中的类和文件OnActivated
中的方法App.xaml.cs
,以及一个公共NavigationHelper
类。
我也在使用MediaCapture
andCaptureElement
在解决方案中,但我未能正确处理该Resuming
事件。这就是我所做的:
我使用NavigationHelper_LoadState
andNavigationHelper_SaveState
方法来启动和停止相机预览(这是LiveCamera.xaml.cs
文件的一部分)。
private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
// Start the camera preview
await StartCameraPreview();
}
private async void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
// Stop the camera preview
await StopCameraPreview();
}
这在应用程序内的页面之间导航时效果很好,但不会在暂停/恢复事件时停止/重新启动相机。
我通过添加App.xaml.cs
以下方法来处理 Resuming 事件来解决此问题(SuspensionManager
负责在 Resuming 应用程序时调用该NavigationHelper_LoadState
方法):
async void App_Resuming(object sender, object e)
{
await SuspensionManager.RestoreAsync();
}
上面的代码在附加了 Visual Studio 的情况下运行良好(在调试和发布模式下):相机预览在接收到挂起/恢复事件时停止/重新启动,并且文件选择器正确返回文件。
但是,如果我在没有 Visual Studio 的情况下执行应用程序(只需从应用程序列表中启动应用程序),相机预览在接收暂停/恢复事件时仍会停止/重新启动,但在使用文件选择器选择文件时,我会看到“正在恢复。 ..”进度条,然后应用程序就崩溃了。
不知何故,选择文件后App_Resuming
和方法发生冲突。OnActivated
我已经通过MessageDialog
在输入每种方法时显示一个来验证这一点(因为我无法使用 Visual Studio 重现问题):在我选择图片后,我在应用程序崩溃之前短暂地看到了 App_Resuming 消息(永远不会看到 OnActivated 消息) . 我没想到会在文件选择器之后调用该方法,因为在执行附加了 VS 的应用程序时不会调用该方法。
为什么在未附加 VS 时会调用不同的(并且据我所知是不正确的)方法?