Caching Views

When you navigate away from a view, by default, all references to the view are removed, and the view will be cleaned up the next time the garbage collector runs. However, this is not ideal if you need to retain the state of the view, such as when the user has entered unsaved data and is navigating away temporarily, or the view has been populated with data that would need to be retrieved again and repopulated.

You can, however, change this default behavior to cache the view and maintain its state by setting the NavigationCacheMode property on the view appropriately. NavigationCacheMode is a property of the Page class, which you can set in XAML. There are three options:

  • Disabled: This will result in the view being destroyed once it has been navigated away from and re-created when it's navigated back to. This is the default value.
  • Enabled: This means that the view will be added to a cache (maintained by the Frame that hosts it) when it is navigated away from. When the user navigates to the view again, this previous instance will be used instead of a new instance being created.
  • Required: Because caching views can be quite memory intensive, the cache stores only a given number of views, configurable by the CacheSize property on the Frame control, after which it removes the oldest entries to be cleaned up by the garbage collector. However, this is not ideal behavior in all scenarios, as you may want to have more control over whether the view can be removed from the cache. To stop a view from automatically being removed from the cache, set the NavigationCacheMode property of the view to Required, instead of Enabled. This will result in it remaining in the cache indefinitely, regardless of the size of the cache.

If you want to retain a view so that data doesn't have to be loaded repeatedly, a value of Enabled would be quite satisfactory. However, if you are retaining unsaved data that you don't want to lose, Required would be a better option in this scenario.

To force a view to be removed from the cache once it is no longer required to be cached, simply set its NavigationCacheMode property to Disabled in your code. When it's navigated away from, it will be removed from the cache. The next time it is navigated to, a new instance of it will be created, and it will go back to using its default value for NavigationCacheMode.

images Note When the view has been retrieved from the cache, rather surprisingly both the Load and NavigatedTo events for the view are still raised when it is navigated to, and there is no property that indicates whether it was retrieved from the cache or that it had previously been loaded. If you have code to populate the view in these events, it would defeat the purpose of caching the view if you were to just populate the view again. The best method to work around this omission is to have a class-level private Boolean variable called isContentLoaded with a default value of false that you can set to true once the view has been populated. You can then check this variable the next time one of these events is raised.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset