Skip to content

How to prevent CameraView's preview from starting on launch #2483

Answered by stephenquan
itsazoo asked this question in Q&A
Discussion options

You must be logged in to vote

Instead of CameraView, you can put a ContentView in your XAML, e.g.

    <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;

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@itsazoo
Comment options

Answer selected by itsazoo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants