0

In a very simple Xamarin android APP I scan a barcode with ZXing and then handle the result. The handle results takes a few seconds to process and during I want to show an ACR showLoading, but the activity indicator hides almost instantly when the scanner/camera is closed.

How can I keep the show loading open while it is handling the result?

Cleaned code:

    async void onAddByScan(object sender, EventArgs e)
    {
        var options = new ZXing.Mobile.MobileBarcodeScanningOptions();
        var scanner = new MobileBarcodeScanner();
        ZXing.Result result = await scanner.Scan(options);
        using (UserDialogs.Instance.Loading())
        {
            await HandleResultAsync(result);
        }
    }

    async Task HandleResultAsync(ZXing.Result result)
    {
        //do something with the results
    }
Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72

1 Answers1

0

You could use InvokeOnMainThreadAsync method in the Xamarin.Essentials: MainThread class. The idea of this method is to not only execute a code on the UI/main thread, but to also to await it's code. This way you can have both async/await logic and main thread execution.

You could refer to the similar case https://stackoverflow.com/a/67483594/10768653.

Leo Zhu
  • 15,726
  • 1
  • 7
  • 23