I am working on a Template 10 MVVM based UWP application.
I am getting the data from an api
and creating a local IEnumerable
property named HugeDataConsumer
on the viewModel
.
After navigation
, to another view, when I press back and comeback to my mainView
, the HugeDataConsumer
IEnumerable
becomes null because the ViewModel
is created again.
I want my ViewModel
to hold the value of the HugeDataConsumer
IEnumerable so that the navigation back event feels like a state resumed.
I have tried setting the NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Required;
on my codeBehind, but I don't think it applies to my viewModel
as well. Is there a way to achieve this?
You can manually serialize your data using DataContractSerializer and deserialize it back when you navigate to the main view model. Prism is using a service called SessionStateService to do such things.
NavigationCacheMode does not apply to the context. It only cashes the view itself, not the model.
You can always cache your data using your own static classes but I think everybody will agree with me if I say developers hate static classes.
Also, if you are developing a UWP app do not forget the suspension scenarios. Whenever your app gets suspended your OnNavigatedTo will fired with a NavigationMode Refresh parameter when the user gets back to the app.