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
When you are navigating forward, can you set NavigationCacheMode to Disabled before you call Frame.Navigate? Then, in OnNavigatedTo() set NavigationCacheMode back to Enabled again.
That should make it so that when you navigate forward, caching is disabled. But when you arrive on the new page instance, OnNavigatedTo would enable it again. When you want to navigate back, you wouldn't touch the NavigationCacheMode before calling Frame.GoBack. That should give you the cached instance, I think.
I believe this would work but I haven't tested it. I'd be curious to know if it does. Interesting scenario there. I'd love to see the app in action and better understand the use of this behavior.