public override void ViewDidAppear(bool animated) { base.ViewDidAppear(animated); if (!cameraView.IsRunning) { cameraView.Start(); } }
private void Grid_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { if (this.IsLoaded && (sender as UIElement).Visibility == Visibility.Visible) { CameraView.Start(); } else { CameraView.Stop(); } }
public async Task StartCamera() { try { if (Device.RuntimePlatform == Device.UWP || !CanUseCamera) { return; } // Already have a camera? if (CameraView != null) { if (!CameraView.EnablePreview) { CameraView.EnablePreview = true; CameraView.Start(); } CameraView.Camera.CaptureQuality = CaptureQuality.High; return; } // Create the camera CameraView = new CameraView() { BackgroundColor = ViewBackgroundColor }; CameraView.CameraOptions.AutoRotateImage = true; // Hook into the events CameraView.PictureReceived += CameraView_PictureReceived; // Listen for tap events TapGestureRecognizer singleTapRecognizer = new TapGestureRecognizer() { NumberOfTapsRequired = 1 }; singleTapRecognizer.Tapped += CameraView_Tapped; CameraView.GestureRecognizers.Add(singleTapRecognizer); // Add the camera await Device.InvokeOnMainThreadAsync(() => CameraContainer.Content = CameraView); CameraView.Camera.CaptureQuality = CaptureQuality.High; } catch (Exception ex) { await DisplayAlert("Error", $"Unable to use camera: {ex.Message}", "OK"); } }