1

这适用于从文件选取器示例创建的 Windows 通用应用程序。该示例的基本代码包括ContinuationManagerWindows Phone 项目中的类和文件OnActivated中的方法App.xaml.cs,以及一个公共NavigationHelper类。

我也在使用MediaCaptureandCaptureElement在解决方案中,但我未能正确处理该Resuming事件。这就是我所做的:

我使用NavigationHelper_LoadStateandNavigationHelper_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 时会调用不同的(并且据我所知是不正确的)方法?

4

1 回答 1

0

存在问题是因为您在 Page 的构造函数中运行FileOpenPicker。那不是什么好事。为了测试,我在您的 LoadPhoto 页面中提供了一个按钮:

在 XAML 中:

<Grid>
    <Button Name="myButton" Content="Click me"/>
    <Image x:Name="Image" Stretch="Uniform"/>
</Grid>

在构造函数中:

public LoadPhoto()
{
    this.InitializeComponent();
    this.navigationHelper = new NavigationHelper(this);
    myButton.Click += (sender, e) => LaunchPicker();
}

您可以在此处下载代码。

也许更好的是,您可以先选择一个文件,然后导航到一个页面。

于 2014-09-11T07:10:44.720 回答