How to go to main page if the user clicked back button using ZXingScannerPage in C# xamarin forms android?

Viewed 32

I'm working with C# xamarin forms Android and I'm trying to make the user go back to HomePage with this "Shell.Current.GoToAsync($"//{nameof(HomePage)}")" when the user click the back button if he has not scanned a QR. But I am not able to do it.

How could I do that?

Thank you very much!!

this is my ScanPage.xaml.cs code:

public async void executeScanner()
    {
        try
        {
            await Scanner();
            if (controlScanner == false)
            {
                await DisplayAlert("Error", "Please scan a QR.\n try again.", "OK");
                checkDoubleCalled = true;
                await Shell.Current.GoToAsync($"//{nameof(HomePage)}");
            }
            else
            {
                takePhotos();
            }
        }
        catch (Exception ex)
        {

        }

private async Task Scanner()
    {
        try
        {
                
            var scannerPage = new ZXingScannerPage();
            scannerPage.AutoFocus();
            scannerPage.Title = "Escanear remito";

            using (SemaphoreSlim semaphoreSlim = new SemaphoreSlim(0, 1))
            {
                //Apertura lambda
                scannerPage.OnScanResult += (result) =>
                {

                    //Si ya obtuvo un resultado, lo asigno a falso
                    scannerPage.IsScanning = false;

                    string resultado = Convert.ToString(result);

                    if (resultado.Contains("http") || resultado.Contains("https") || !resultado.Contains("\t"))
                    {
                        Navigation.PopAsync();
                        this.DisplayAlert("QR not valid", "El QR is not valid", "OK");
                    }
                    else
                        Device.BeginInvokeOnMainThread(async () =>
                        {
                            try
                            {

                                await Application.Current.MainPage.Navigation.PopAsync();
                                subs = result.Text.Split('\t');
                                string cadena = "var1N°: " + subs[0] + "\nvar2 N°: " + subs[1] + "\nvar3 N°: " + subs[2];
                                num.Text = "N° var12: " + subs[1];
                                controlScanner = true;

                                await this.DisplayAlert("Data QR", cadena, "OK");
                                
                                
                            }
                            catch (Exception ex)
                            {
                                checkDoubleCalled = true;
                                controlScanner = false;
                                await DisplayAlert("Error", "QR not valid.\n try again", "OK");
                                await Shell.Current.GoToAsync($"//{nameof(HomePage)}");
                            }
                            semaphoreSlim.Release();
                        });
                };
                
                await Navigation.PushAsync(scannerPage);
                await semaphoreSlim.WaitAsync();
            }
            
        }
        catch(Exception ex)
        {
            checkDoubleCalled = true;
            controlScanner = false;
            await DisplayAlert("Error", "I'm sorry, we had a problem", "OK");
            await Shell.Current.GoToAsync($"//{nameof(HomePage)}");
        }
        return;
    }
0 Answers
Related