diff --git a/BlazorBarcodeScanner.ZXing.JS/BarcodeReader.razor.cs b/BlazorBarcodeScanner.ZXing.JS/BarcodeReader.razor.cs index efae01c..f8e7cfa 100644 --- a/BlazorBarcodeScanner.ZXing.JS/BarcodeReader.razor.cs +++ b/BlazorBarcodeScanner.ZXing.JS/BarcodeReader.razor.cs @@ -120,6 +120,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender) BarcodeReaderInterop.BarcodeReceived += ReceivedBarcodeText; BarcodeReaderInterop.ErrorReceived += ReceivedErrorMessage; + BarcodeReaderInterop.DecodingStarted += DecodingStarted; + BarcodeReaderInterop.DecodingStopped += DecodingStopped; + if (StartCameraAutomatically && _videoInputDevices.Count > 0) { await _backend.SetVideoInputDevice(SelectedVideoInputId); @@ -136,13 +139,18 @@ protected override async Task OnAfterRenderAsync(bool firstRender) [Obsolete("Please use DisposeAsync")] public void Dispose() { - StopDecoding(); + DisposeAsync(); } public async ValueTask DisposeAsync() { try { await StopDecoding(); + + BarcodeReaderInterop.BarcodeReceived -= ReceivedBarcodeText; + BarcodeReaderInterop.ErrorReceived -= ReceivedErrorMessage; + BarcodeReaderInterop.DecodingStarted -= DecodingStarted; + BarcodeReaderInterop.DecodingStopped -= DecodingStopped; } catch (Exception ex) { @@ -170,7 +178,6 @@ public async Task StartDecoding() var height = StreamHeight ?? 0; await _backend.StartDecoding(_video, width, height); SelectedVideoInputId = await _backend.GetVideoInputDevice(); - IsDecoding = true; StateHasChanged(); } @@ -200,7 +207,6 @@ public async Task StopDecoding() { BarcodeReaderInterop.OnBarcodeReceived(string.Empty); await _backend.StopDecoding(); - IsDecoding = false; StateHasChanged(); } @@ -266,6 +272,17 @@ private async Task ReceivedErrorMessage(ErrorReceivedEventArgs args) StateHasChanged(); } + private Task DecodingStarted(DecodingActionEventArgs _) + { + IsDecoding = true; + return Task.CompletedTask; + } + private Task DecodingStopped(DecodingActionEventArgs _) + { + IsDecoding = false; + return Task.CompletedTask; + } + protected async Task ChangeVideoInputSource(string deviceId) { SelectedVideoInputId = deviceId;