C# 在摄像头完全初始化之前锁定手机。当我解锁手机时,它会断开
我正在开发一个摄像头应用程序,当我在摄像头完全初始化之前离开应用程序并尝试返回应用程序时遇到问题。 在我的on导航from方法中,我有:C# 在摄像头完全初始化之前锁定手机。当我解锁手机时,它会断开,c#,windows-phone-8,camera,windows-phone,C#,Windows Phone 8,Camera,Windows Phone,我正在开发一个摄像头应用程序,当我在摄像头完全初始化之前离开应用程序并尝试返回应用程序时遇到问题。 在我的on导航from方法中,我有: protected override void OnNavigatedFrom(NavigationEventArgs e) { try { if (cameraInit) { Dispatcher.Begin
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
try
{
if (cameraInit)
{
Dispatcher.BeginInvoke(() => {
if (cam != null)
{
cam.Dispose();
cam.CaptureCompleted -= cam_CaptureCompleted;
cam.CaptureImageAvailable -= cam_CaptureImageAvailable;
cam.AutoFocusCompleted -= cam_AutoFocusCompleted;
CameraButtons.ShutterKeyPressed -= OnButtonFullPress;
cam = null;
cameraInit = false;
}
});
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是我的导航到的方法:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// Check to see if the camera is available on the device.
if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true)
{
if (cam == null)
{
cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
//Create the camera event handlers
//// Event is fired when the capture sequence is complete.
cam.CaptureCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_CaptureCompleted);
//// Event is fired when the capture sequence is complete and an image is available.
cam.CaptureImageAvailable += new EventHandler<Microsoft.Devices.ContentReadyEventArgs>(cam_CaptureImageAvailable);
//// The event is fired when auto-focus is complete.
cam.AutoFocusCompleted += new EventHandler<CameraOperationCompletedEventArgs>(cam_AutoFocusCompleted);
//// The event is fired when the shutter button receives a full press.
cam.Initialized += cam_Initialized;
CameraButtons.ShutterKeyPressed += OnButtonFullPress;
}
////Set the VideoBrush source to the camera.
canvasCamBrush.SetSource(cam);
}
主要问题是,当启动onNavigatedFrom方法时,cam未完全初始化,然后当我返回应用程序时,它在OnNavigatedTo方法上中断,因此我需要在那里等待,直到启动cam\u initialized方法,然后再运行onNavigatedFrom
下面是另一个人的例子,他有这个问题,但我无法让它工作:
谢谢大家,,
L在离开应用程序之前,我通过一个“无限循环”解决了这个问题,直到cam完全初始化。OnNavigatedFrom的代码是:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
try
{
while (!cameraInit)
{
System.Threading.Thread.Sleep(100);
}
if (cameraInit)
{
Dispatcher.BeginInvoke(() => {
if (cam != null)
{
cam.Dispose();
cam.CaptureCompleted -= cam_CaptureCompleted;
cam.CaptureImageAvailable -= cam_CaptureImageAvailable;
cam.AutoFocusCompleted -= cam_AutoFocusCompleted;
CameraButtons.ShutterKeyPressed -= OnButtonFullPress;
cam = null;
cameraInit = false;
}
});
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是我能找到的最好的解决办法。希望能对别人有所帮助。
谢谢
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
try
{
while (!cameraInit)
{
System.Threading.Thread.Sleep(100);
}
if (cameraInit)
{
Dispatcher.BeginInvoke(() => {
if (cam != null)
{
cam.Dispose();
cam.CaptureCompleted -= cam_CaptureCompleted;
cam.CaptureImageAvailable -= cam_CaptureImageAvailable;
cam.AutoFocusCompleted -= cam_AutoFocusCompleted;
CameraButtons.ShutterKeyPressed -= OnButtonFullPress;
cam = null;
cameraInit = false;
}
});
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}