diff --git a/ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs b/ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs index 971f42f..8727dd7 100644 --- a/ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs +++ b/ZXing.Net.MAUI/CameraBarcodeReaderViewHandler.cs @@ -51,17 +51,23 @@ protected override async void ConnectHandler(NativePlatformCameraPreviewView nat { base.ConnectHandler(nativeView); - if (await cameraManager.CheckPermissions()) - cameraManager.Connect(); + if (cameraManager != null) + { + if (await cameraManager.CheckPermissions()) + cameraManager.Connect(); - cameraManager.FrameReady += CameraManager_FrameReady; + cameraManager.FrameReady += CameraManager_FrameReady; + } } protected override void DisconnectHandler(NativePlatformCameraPreviewView nativeView) { - cameraManager.FrameReady -= CameraManager_FrameReady; + if (cameraManager != null) + { + cameraManager.FrameReady -= CameraManager_FrameReady; - cameraManager.Disconnect(); + cameraManager.Disconnect(); + } base.DisconnectHandler(nativeView); } @@ -70,7 +76,7 @@ private void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs { VirtualView?.FrameReady(e); - if (VirtualView.IsDetecting) + if (VirtualView?.IsDetecting ?? false) { var barcodes = BarcodeReader.Decode(e.Data); diff --git a/ZXing.Net.MAUI/CameraViewHandler.cs b/ZXing.Net.MAUI/CameraViewHandler.cs index 239fc9a..9a20992 100644 --- a/ZXing.Net.MAUI/CameraViewHandler.cs +++ b/ZXing.Net.MAUI/CameraViewHandler.cs @@ -44,11 +44,14 @@ protected override NativePlatformCameraPreviewView CreatePlatformView() protected override async void ConnectHandler(NativePlatformCameraPreviewView nativeView) { base.ConnectHandler(nativeView); - - if (await cameraManager.CheckPermissions()) - cameraManager.Connect(); - - cameraManager.FrameReady += CameraManager_FrameReady; + + if (cameraManager != null) + { + if (await cameraManager.CheckPermissions()) + cameraManager.Connect(); + + cameraManager.FrameReady += CameraManager_FrameReady; + } } void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs e) @@ -56,9 +59,12 @@ void CameraManager_FrameReady(object sender, CameraFrameBufferEventArgs e) protected override void DisconnectHandler(NativePlatformCameraPreviewView nativeView) { - cameraManager.FrameReady -= CameraManager_FrameReady; + if (cameraManager != null) + { + cameraManager.FrameReady -= CameraManager_FrameReady; - cameraManager.Disconnect(); + cameraManager.Disconnect(); + } base.DisconnectHandler(nativeView); }