WinRT Passing ViewModel between pages

2019-09-08 01:05发布

问题:

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.

回答1:

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.



回答2:

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.



标签: winrt-xaml