我的UWP应用程序中有一个摄像头功能,该功能的一部分是实际检查是否有任何连接的摄像头设备。我按照这组示例开发了该功能:CameraStarterKit
当我在没有网络摄像头或摄像头的机器上测试代码时,将显示消息提示。但是我认为当我退出页面时检查是否有相机设备的代码仍在运行,因为当我最小化应用程序并再次打开它时,提示出现了。
当我导航到另一页时,我是否可以停止检查相机是否已连接的代码?
以下是检查相机是否已连接的代码:
private async Task InitializeCameraAsync()
{
Debug.WriteLine("InitializeCameraAsync");
if (_mediaCapture == null)
{
// Attempt to get the back camera if one is available, but use any camera device if not
var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);
if (cameraDevice == null)
{
this.LoadProgressRing.IsActive = false;
this.LoadProgressStackPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
MessageDialog cameraError = new MessageDialog("Connection Problem. No camera device found. Please kindly contacct the administrator.");
UICommand YesBtn = new UICommand("Ok", delegate (IUICommand command)
{
idleTimer.Stop();
var rootFrame = (Window.Current.Content as Frame);
rootFrame.Navigate(typeof(HomePage));
rootFrame.BackStack.Clear();
});
cameraError.Commands.Add(YesBtn);
await cameraError.ShowAsync();
Debug.WriteLine("No camera device found!");
return;
}
// Create MediaCapture and its settings
_mediaCapture = new MediaCapture();
var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id };
// Initialize MediaCapture
try
{
await _mediaCapture.InitializeAsync(settings);
_isInitialized = true;
}
catch (UnauthorizedAccessException)
{
Debug.WriteLine("The app was denied access to the camera");
}
// If initialization succeeded, start the preview
if (_isInitialized)
{
// Figure out where the camera is located
if (cameraDevice.EnclosureLocation == null || cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown)
{
// No information on the location of the camera, assume it's an external camera, not integrated on the device
_externalCamera = true;
}
else
{
// Camera is fixed on the device
_externalCamera = false;
// Only mirror the preview if the camera is on the front panel
_mirroringPreview = (cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
}
// Initialize rotationHelper
_rotationHelper = new CameraRotationHelper(cameraDevice.EnclosureLocation);
_rotationHelper.OrientationChanged += RotationHelper_OrientationChanged;
await StartPreviewAsync();
UpdateCaptureControls();
}
return;
}
}
修改
SetUpBasedOnStateAsync()方法的代码:
private async Task SetUpBasedOnStateAsync()
{
// Avoid reentrancy: Wait until nobody else is in this function.
while (!_setupTask.IsCompleted)
{
await _setupTask;
}
// We want our UI to be active if
// * We are the current active page.
// * The window is visible.
// * The app is not suspending.
bool wantUIActive = _isActivePage && Window.Current.Visible && !_isSuspending;
if (_isUIActive != wantUIActive)
{
_isUIActive = wantUIActive;
Func<Task> setupAsync = async () =>
{
if (wantUIActive)
{
await SetupUiAsync();
await InitializeCameraAsync();
}
else
{
await CleanupCameraAsync();
await CleanupUiAsync();
}
};
_setupTask = setupAsync();
}
await _setupTask;
}
答案 0 :(得分:0)
当我导航到另一页时,我是否可以停止检查相机是否已连接的代码?
当您导航到另一个页面时,将调用OnNavigatingFrom处理程序方法,您可以看到将在其中调用SetUpBasedOnStateAsync
方法。在此方法中,将调用InitializeCameraAsync
方法。因此,您只需要在SetUpBasedOnStateAsync
方法中更改一些行代码。
但我认为当我退出页面时检查是否有相机设备的代码仍然在运行,因为当我最小化应用程序并再次打开它时,提示出现。
这是正常行为。当您最小化并打开您的应用时,代码将进入此if块。如果您不想要此行为。我建议您可以创建一个新项目,而不是更改官方代码示例。因为你需要改变许多地方。