How to go to main page after buttom back with ZXingScannerPage in C# xamarin forms android?

Viewed 23

I'm working with C# xamarin forms android and I'm trying to go back using OnBackButtonPressed. It kind of works but my problem is that the user has to touch 2 times to go back. I will left a photo below to explain my point. But basically the user goes from Home page to Scaner page and when he touch buttom back when he is scanning doesn't come back to home page. He has to touch 2 times and i don't want that. I need that when he touches the buttom back he goes to the home page.

my scanPage.xaml.cs:

protected override bool OnBackButtonPressed()
    {
        try
        {
            if (Shell.Current.FlyoutIsPresented == false)
            {
                Shell.Current.GoToAsync($"//{nameof(HomePage)}");
            }
            else
            {
                Shell.Current.FlyoutIsPresented = false;
            }
        }
        catch(Exception ex)
        {

        }

        //return base.OnBackButtonPressed();
        return true;
    }
private async Task Scanner()
    {
        try
        {
            var scannerPage = new ZXingScannerPage();
            scannerPage.AutoFocus();
            scannerPage.Title = "Scanner";
            using (SemaphoreSlim semaphoreSlim = new SemaphoreSlim(0, 1))
            {
                scannerPage.OnScanResult += (result) =>
                {
                    scannerPage.IsScanning = false;
                    string resultado = Convert.ToString(result);

                    if (resultado.Contains("http") || resultado.Contains("https") || !resultado.Contains("\t"))
                    {
                        Navigation.PopAsync();
                        this.DisplayAlert("Error", "QR not valid", "OK");
                    }
                    else
                        Device.BeginInvokeOnMainThread(async () =>
                        {
                            try
                            {
                                await Application.Current.MainPage.Navigation.PopAsync();
                                subs = result.Text.Split('\t');
                                string cadena = "var 1: " + subs[0] + "\nvar 2: " + subs[1] + "\nvar 3: " + subs[2];
                                numEntrega.Text = "var 2: " + subs[1];
                                controlScanner = true;
                                await this.DisplayAlert("Data", cadena, "OK");
                                
                                
                            }
                            catch (Exception ex)
                            {
                                checkDoubleCalled = true;
                                controlScanner = false;
                                numEntrega.Text = "";
                                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();
            }
            return;
        }
        catch(Exception ex)
        {
            checkDoubleCalled = true;
            controlScanner = false;
            numEntrega.Text = "";
            await DisplayAlert("Error", "Sorry we had a problem.", "OK");
            await Shell.Current.GoToAsync($"//{nameof(HomePage)}");
        }
        return;
    }

Image of my problem:

enter image description here

How could I resolve this problem? Thank you very much!

0 Answers
Related