Zea Shah
Zea Shah

Reputation: 382

How to ignore same page in navigation history

Is there anyway that we can ignore same page entry in navigation stack, Actually I have a following situation in my UWP App, this is how user is navigating from one page to another

MainPage - > Page 1 -> Page 2 - >

Now from Splitview user click on Page 1, and so stack goes like this

MainPage -> Page 1 - > Page 2 -> Page 1

What I want is that if Page 1 is already loaded than I want to remove Page 1 and all the Frame above that page,

This is what stack should look like:

MainPage -> Page 1

In android I know that we can set flag "ClearTop" so that it automatically performs clean up for the same page, is there something like that for UWP App?

Upvotes: 2

Views: 312

Answers (1)

Sébastien Pertus
Sébastien Pertus

Reputation: 715

On the current frame, you can check the BackStack collection, and remove any entry

Here is a snippet you can use, and adapt :)

     Frame rootFrame = Window.Current.Content as Frame;

        if (rootFrame.CanGoBack)
        {
            // Check if we are on the entry page and try to go back
            var backTypePage = rootFrame.BackStack[rootFrame.BackStackDepth - 1];

            if (this.EntryPage != null && backTypePage.SourcePageType == this.EntryPage)
            {
                e.Handled = false;
                rootFrame.BackStack.Clear();
            }
            else
            {
                e.Handled = true;
                rootFrame.GoBack();
            }
        }

Upvotes: 0

Related Questions