question

GaneshGebhard-8778 avatar image
0 Votes"
GaneshGebhard-8778 asked JarvanZhang-MSFT commented

Xamarin.Forms - Crash with Navigation.RemovePage

Hey!

I had some issues with the use of Navigation in my Xamarin.Forms app on a barcode scanner page, which was luckily solved here: https://docs.microsoft.com/en-us/answers/questions/520601/xamarinforms-navigationpushasync-34only-the-origin.html

Now I have a new problem with this change, which I didn't had before. When a barcode is scanned on this page, based on the page where the user is coming from (SourcePage), the app navigates to the correct next page with the results. Somehow the app does this perfectly the first time, but it crashes the second time.
Let's say that I open the barcode scanner from the MainPage which will direct me to Page1 after the scan, the stack will look like MainPage -> BarcodeScannerPage -> Page1. The problem with this is that when the user presses the back-button on Page1, the app returns to the BarcodeScannerPage obviously. To make sure it goes back to the main page, I remove the BarcodeScannerPage in the stack which leads to: MainPage -> Page1. Now the user returns to the MainPage as it should be.

Here comes the problem: the first time this works, the second time it crashes with the following exception:

124852-image.png

This error is pretty strange, since it implies that there is less items in the stack than the code is asking for, but if I open de barcode scanner the first time leading to a stack of MainPage -> BarcodeScannerPage -> Page1, I suppose this stack will be created again when I open the barcode scanner for a second time from the MainPage.

Does someone know how to solve this? Is there maybe a better way to remove the page from the stack?

dotnet-csharpdotnet-xamarin
image.png (37.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

Hello GaneshGebhard-8778,​

Welcome to our Microsoft Q&A platform!

I suppose this stack will be created again when I open the barcode scanner for a second time from the MainPage.

You could the navigation stack to check the page list.

Does someone know how to solve this? Is there maybe a better way to remove the page from the stack?

Try removing the page from the NavigationStack by the 'BarcodeScannerPage' class name. Please check the code:

var list = Navigation.NavigationStack;
foreach (Page item in list)
{
    var name = item.GetType().Name;
    if (name == typeof(BarcodeScannerPage).Name)
    {
        Navigation.RemovePage(item);

        return;
    }
}

Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, @GaneshGebhard-8778
I have not heard from you for a couple of days. Please let me know if there is anything that I can help here.

0 Votes 0 ·