How to prevent CameraView's preview from starting on launch #2483
-
I am using the CameraView and MediaPicker in my maui app. Both are accessed on the same page because I serve the frontend through HybridWebView. In XAML I have the CameraView hidden and disabled but I noticed when I run the app on Windows the camera is on and used by CameraView however it's not visible as it should be. I don't want the CameraView to start preview by default because it would prevent users from accessing the camera through the MediaPicker. Is there an option to disable the Camera preview on start? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Instead of <ContentView x:Name="contentView" /> Then, in your code behind, you can load your CameraView when you need it, e.g. // using CommunityToolkit.Maui.Views;
private CameraView? cameraView = null;
private void OnStartCamera(object sender, EventArgs e)
{
if (cameraView is null)
{
contentView.Content = cameraView = new CameraView();
}
} Then, when you need to stop, you can call StopCameraPreview() before you unload it, e.g. private void OnStopCamera(object sender, EventArgs e)
{
if (cameraView is not null)
{
cameraView.StopCameraPreview();
contentView.Content = cameraView = null;
}
} |
Beta Was this translation helpful? Give feedback.
Instead of
CameraView
, you can put aContentView
in your XAML, e.g.Then, in your code behind, you can load your CameraView when you need it, e.g.
Then, when you need to stop, you can call StopCameraPreview() before you unload it, e.g.