WPF Prism Inject same viewmodel instance into two views

╄→尐↘猪︶ㄣ 提交于 2019-12-21 05:43:04

问题


So I have two separate views in a WPF Prism app. How can I inject the same instance of a ViewModel into both the views via Dependency Injection?


回答1:


(I assume you are using Unity as you mentioned DI directly and are not saying "Import" or similar)

Hi,

You will have to register the ViewModel as a singleton (using a ContainerControlledLifetimeManager) in the container and have it injected as usual. This ensures that you will get the same instance whenever you request for it. A valid approach in this scenario might be to use a named registration in case you want to get the same VM injected elsewhere, but you don't need the same instance.

This article explains how to do this.



来源:https://stackoverflow.com/questions/3957383/wpf-prism-inject-same-viewmodel-instance-into-two-views

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