Bug 55005 - Xamarin.Forms Page Navigation Stack is different from UWP Navigation Stack
Summary: Xamarin.Forms Page Navigation Stack is different from UWP Navigation Stack
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows ()
Version: 2.2.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-04-11 08:52 UTC by Marco Biasini
Modified: 2017-04-18 07:49 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Sample that illustrates the issue (158.94 KB, application/zip)
2017-04-11 08:52 UTC, Marco Biasini
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Marco Biasini 2017-04-11 08:52:06 UTC
Created attachment 21416 [details]
Sample that illustrates the issue

We are developing a NuGet Package that uses a native UWP page to display its content. The UWP Page is pushed on top of the top-most Xamarin.Forms page using 

var frame = Window.Current.Content as Frame;
frame.Navigate(typeof(PageToNavigateTo));

Once the user is done, the native page is discarded with

var frame = Window.Current.Content as Frame;
frame.GoBack();

We would expect the app to go back to the page where the native page was opened from. However, the application goes back to the main page of the app. Is this expected behavior? It appears as if the navigation stack from Xamarin.Forms is separate from the native UWP navigation stack. Is this observation correct? If so, how can we access the Xamarin.Forms navigation stack from UWP native code?
Comment 1 Jimmy [MSFT] 2017-04-17 16:59:37 UTC
Xamarin.Forms does not use the same navigation pattern as non-Forms apps so it does keep its own internal navigation stack[1]. 

This means that MainPage.xaml from the UWP project is the only page on the native navigation stack until you push NativePage.xaml. However this isn't the reason why this is happening. As long as MainPage.xaml maintains its state, any navigation you've done in Forms will be reflected when you return to the page.

The real reason for this issue is that page content and state are not cached by default on UWP[2] so when you returned to MainPage, it reloaded its content which in this case meant reloading the Xamarin Forms app. To enable caching you need to add this in the MainPage.xaml.cs constructor:

> NavigationCacheMode = NavigationCacheMode.Required;


[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.WinRT/NavigationPageRenderer.cs#L391
[2] https://docs.microsoft.com/en-us/windows/uwp/layout/navigate-between-two-pages#cache-a-page
Comment 2 Marco Biasini 2017-04-18 07:49:00 UTC
Thanks for the explanation! That solved the issue.