WinRT Passing ViewModel between pages

梦想与她 提交于 2019-12-12 05:17:34

问题


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.



来源:https://stackoverflow.com/questions/14802728/winrt-passing-viewmodel-between-pages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!