Skip to content Skip to sidebar Skip to footer

Processing Dialog After QR Scan In Xamarin

I'm using QR Code Scanner in Xamarin App, when it scans the qr code, there are some operation it does which takes about a minute, while it's performing operation, I want to show a

Solution 1:

You can use Xamarin.Essentials' MainThread class and more specifically - the InvokeOnMainThreadAsync method. 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.

try
{
    // QR Scan Result
    string qr_scan = result.Response;
    var result = JsonConvert.DeserializeObject<QRScan>(qr_scan);
    await MainThread.InvokeOnMainThreadAsync(() => CreateConnection());
}
catch (Exception error)
{ }
finally
{
    // navigate to next page
    await NavigationService.NavigateToAsync<NextViewModel>();
}

Keep in mind that if the method CreateConnection takes a long time to execute, then it would be better to execute on the main thread only the dialog presentation (UserDialogs.Instance.Loading("")).


Solution 2:

Try something like this

Device.BeginInvokeOnMainThread(async () =>
{

    try
    {
        using (UserDialogs.Instance.Loading(("Processing")))
        {
            await Task.Delay(300);
                      
            //Your Service code
        }
    }
    catch (Exception ex)
    {
        var val = ex.Message;
        UserDialogs.Instance.Alert("Test", val.ToString(), "Ok");
    }
});

Post a Comment for "Processing Dialog After QR Scan In Xamarin"