C# WindowsPhone PhoneCamera-页面恢复时出现异常

C# WindowsPhone PhoneCamera-页面恢复时出现异常,c#,windows-phone-8,camera,aggregateexception,C#,Windows Phone 8,Camera,Aggregateexception,我有一个奇怪的例外。我在一个页面中使用PhoneCamera API,用户可以制作一些phothos,在捕获后直接上传 我的代码: protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); _mediaLibrary = new MediaLibrary(); // Initialize the camera object _phoneCamera

我有一个奇怪的例外。我在一个页面中使用PhoneCamera API,用户可以制作一些phothos,在捕获后直接上传

我的代码:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    _mediaLibrary = new MediaLibrary();

    // Initialize the camera object
    _phoneCamera = new PhotoCamera(CameraType.Primary);
    _phoneCamera.Initialized += CamInitialized;
    _phoneCamera.AutoFocusCompleted += PhoneCamera_AutoFocusCompleted;
    _phoneCamera.CaptureImageAvailable += PhoneCamera_CaptureImageAvailable;

    // Display the camera feed in the UI
    ViewfinderBrush.SetSource(_phoneCamera);

    // Rotation for transform
    ViewfinderTransform.Rotation = _phoneCamera.Orientation;
}

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    if (_phoneCamera != null)
    {
        // Cleanup
        _phoneCamera.Dispose();
        _phoneCamera.Initialized -= CamInitialized;
        _phoneCamera.AutoFocusCompleted -= PhoneCamera_AutoFocusCompleted;
        _phoneCamera.CaptureImageAvailable -= PhoneCamera_CaptureImageAvailable;

        if (_cameraInizialized)
        {
            CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed;
            CameraButtons.ShutterKeyPressed -= CameraButtons_ShutterKeyPressed;
        }
    }

    base.OnNavigatingFrom(e);
}

private void CamInitialized(object sender, CameraOperationCompletedEventArgs e)
{
    if (e.Succeeded)
    {
        // Set flashmode
        Dispatcher.BeginInvoke(() => {  _phoneCamera.FlashMode = FlashMode.Auto; });

        // Subscribe to hardeware-button-events
        CameraButtons.ShutterKeyHalfPressed += CameraButtons_ShutterKeyHalfPressed;
        CameraButtons.ShutterKeyPressed += CameraButtons_ShutterKeyPressed;

        _cameraInizialized = true;
    }
    else
    {
        // Show error message
    }
}
触发初始化事件后,一切正常。但我得到了一个例外,当用户点击手机的主页按钮,而相机正在初始化。(未触发初始化事件)

当用户使用“后退”按钮返回时,会发生以下异常:

[Type]:[AggregateException]
[ExceptionMessage]:[One or more errors occurred.]
[StackTrace]:[
    at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
    at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
    at Microsoft.Devices.Camera.OpenCaptureDevice()
    at Microsoft.Devices.Camera.<>c__DisplayClass2.<InitializeVideoSession>b__0()]
[InnerExceptions]:[[[Type]:[InvalidOperationException]
    [Message]:[The text associated with this error code could not be found.
    Unable to acquire the camera. You can only use this class while in the foreground.]
[Type]:[AggregateException]
[例外消息]:[发生了一个或多个错误。]
[StackTrace]:[
位于System.Threading.Tasks.Task.ThrowifeException(布尔值IncludeTaskCanceledException)
在System.Threading.Tasks.Task.Wait(Int32毫秒计时,CancellationToken CancellationToken)
在Microsoft.Devices.Camera.OpenCaptureDevice()上
在Microsoft.Devices.Camera.c__DisplayClass2.b__0()中]
[InnerExceptions]:[[[Type]:[InvalidOperationException]
[消息]:[找不到与此错误代码关联的文本。
无法获取相机。您只能在前台使用此类。]
我尝试过很多其他的选择,但都没有效果。我现在不知道为什么会发生这个错误。虽然我知道它发生在哪里,在什么情况下


欢迎提供任何帮助。谢谢。

PhoneCamera中存在已知的应用程序崩溃错误。当应用程序未初始化时,从camera页面返回时,应用程序将崩溃。您可以参考