WinRT/UWP Frame and Page caching: How to create new page instance on Navigate() and keep the page instance on GoBack()
Because there was no solution to this problem, I had to reimplement all paging relevant classes: Page, Frame, SuspensionManager, etc...
The solution can be downloaded here: https://github.com/MyToolkit/MyToolkit/wiki/Paging-Overview
Update:
The page class now also provides the OnNavigatingFromAsync method to show for example an async popup and cancel navigation if required...
I had much the same problem. I wanted it such that when I was moving forward in Metro (Windows Store to be proper), it would create a new instance. However, when going back, it would keep the data that I wanted save.
So, I likewise used NavigationCacheMode = NavigationCacheMode.Enabled. I found that regardless of which way I was traversing, forward or backward, everything was always saved. So, I would go forward several pages, then step back a few. Hoping everything was reset as I moved forward, I invariably found that it wasn't; it had retained the data.
I tried everything, including writing my own back button code to include NavigationCacheMode = NavigationCacheMode.Disabled, but to no avail. As others have pointed out, once you have enabled it, the NavigationCacheMode simply won't disable.
I did find a solution. I went to the LayoutAwarePage.cs and simply made a minor change. Under the "OnNavigatedTo" I found the line:
// Returning to a cached page through navigation shouldn't trigger state loading
if (this._pageKey != null) return;
However, the comment ran contrary to what I wanted. I was looking for state loading in a unidirectional pattern. If moving forward, I wanted state loading; if moving backward I wanted the behavior the comment indicated - no state loading.
So I simply modified the line.
// Returning to a cached page through navigation shouldn't trigger state loading
if (this._pageKey != null && e.NavigationMode == NavigationMode.Back) return;
I have tested it and it works perfectly. Now, when navigating backwards it remembers the state and keeps the page the same. Navigating forward, it loads fresh.
Perhaps not best practice, but I do not call "OnNavigatedTo" from my code-behind. I do everything through the "LoadState." If you are overriding "OnNavigatedTo" in the code-behind you might see different behavior.
Thank you,
Joseph Irvine