Richard Maxwell
Richard Maxwell

Reputation: 538

WinRT Passing ViewModel between pages

I have a two page Windows 8 application. A view page and a settings page. Both pages use the same view model. I can pass the view model between pages to work with it, but then I can not save state because I have passed a complex type between pages (it has to do with how they keep references, and stops you from saving).

So I guess I need to save and reload my view model on each page? I can't find a good reference on what is a best practice for this.

Upvotes: 0

Views: 764

Answers (2)

Kaylyn Bogle
Kaylyn Bogle

Reputation: 93

You can use Frame to save ViewModel state between page navigation. I have a similar situation in an app I'm writing (same viewmodel for different pages) and it works perfectly. It'll even cache pages for you.

I blogged about using Frame with ViewModels recently, but it's more about setting up frame interaction than navigation/caching behavior.

Upvotes: 0

Jawahar
Jawahar

Reputation: 4885

Usually WinRT navigates through Root frame, which initialized in App.cs. It is better to define a Frame object in MainPage and navigate all your pages within this frame. Your frame datacontext can bind to ViewModel.

Upvotes: 2

Related Questions