当我在Windows Phone 8中调用等待PhotoCaptureDevice.OpenAsync(..)时,相机应用会抛出InvalidOperationException。
System.InvalidOperationException:由于操作无效 对象的当前状态。在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()at JPMC.Controls.CaptureTaskLauncher.d_6.MoveNext()---堆栈结束 从抛出异常的先前位置追踪--- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.b_0(对象 状态)}
但是还有一件事就是每当我第一次运行相机测试时它会完美地工作,即PhotoCaptureDevice正确初始化,但是当我从另一个页面返回到Camera测试时,它会给出上面提到的错误。 那么,任何人都可以帮我解决这个问题吗?
由于
答案 0 :(得分:0)
您是否尝试在离开页面之前使用PhotoCaptureDevice.Close()和Dispose()来释放资源?
答案 1 :(得分:0)
private static PhotoCaptureDevice d = null;
public static async Task<bool> InitializeCamera(CameraSensorLocation sensorLocation)
{
bool catchValue = true;
try
{
Windows.Foundation.Size initialResolution = new Windows.Foundation.Size(640, 480);
Windows.Foundation.Size previewResolution = new Windows.Foundation.Size(640, 480);
Windows.Foundation.Size captureResolution = new Windows.Foundation.Size(640, 480);
d = await PhotoCaptureDevice.OpenAsync(sensorLocation, initialResolution);
await d.SetPreviewResolutionAsync(previewResolution);
await d.SetCaptureResolutionAsync(captureResolution);
d.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, true);
MemoryStream stream = new MemoryStream();
CameraCaptureSequence sequence = d.CreateCaptureSequence(1);
sequence.Frames[0].CaptureStream = stream.AsOutputStream();
await d.PrepareCaptureSequenceAsync(sequence);
await sequence.StartCaptureAsync();
if (stream.Length > 0)
{
d.Dispose();
d = null;
stream.Dispose();
stream = null;
return true;
}
else
{
d.Dispose();
d = null;
stream.Dispose();
stream = null;
}
return false;
}
catch (Exception ex)
{
catchValue = false;
}
if (!catchValue)
{
Windows.Foundation.Size initialResolution = new Windows.Foundation.Size(640, 480);
Windows.Foundation.Size previewResolution = new Windows.Foundation.Size(640, 480);
Windows.Foundation.Size captureResolution = new Windows.Foundation.Size(640, 480);
d = await PhotoCaptureDevice.OpenAsync(sensorLocation,initialResolution);
await d.SetPreviewResolutionAsync(previewResolution);
await d.SetCaptureResolutionAsync(captureResolution);
d.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, true);
MemoryStream stream = new MemoryStream();
CameraCaptureSequence sequence = d.CreateCaptureSequence(1);
sequence.Frames[0].CaptureStream = stream.AsOutputStream();
await d.PrepareCaptureSequenceAsync(sequence);
await sequence.StartCaptureAsync();
if (stream.Length > 0)
{
d.Dispose();
d = null;
stream.Dispose();
stream = null;
return true;
}
else
{
d.Dispose();
d = null;
stream.Dispose();
stream = null;
}
return false;
}
return false;
}