I\'m trying to create an UWP (Universal Windows App) application with C#. My problem is the Frame control: If I use it without NavigationCacheMode = Requi
I achieved this by:
On clicking on Page2 button/link:
Traverse the Frame BackStack and find out if the Page2 is in BackStack. If Page2 is found call Frame.GoBack() required number of times. If not found just navigate to the new Page. This will work for any no of pages.
Code Sample:
public void Page2Clicked(object sender, RoutedEventArgs e)
{
int isPresent = 0;
int frameCount = 0;
//traverse BackStack in reverse order as the last element is latest page
for(int index= Frame.BackStack.Count-1; index>=0;index--)
{
frameCount += 1;
//lets say the first page name is page1 which is cached
if ("Page2".Equals(Frame.BackStack[index].SourcePageType.Name))
{
isPresent = 1;
//Go back required no of times
while (frameCount >0)
{
Frame.GoBack();
frameCount -= 1;
}
break;
}
}
if (isPresent == 0)
{
Frame.Content = null;
Frame.Navigate(typeof(Page2));
}
}
This will be helpful if forward/backward buttons will not be made much use of. as this solution will affect the forward/backward navigation. If you wish to use forward/backward navigation as well, then some additional cases has to be handled.