MVVM : Share data between ViewModels

前端 未结 5 825
一个人的身影
一个人的身影 2021-01-02 00:25

How do I share data between multiple ViewModels ?

For example there is a class named Project in application .

    public class Project : ModelB         


        
5条回答
  •  醉梦人生
    2021-01-02 00:31

    Don't, don't. Don't use singletons this way in your MVVM application. In fact, the Project class should be a model for your ViewModels. Just pass it in vm's constructor. If you really need to share one instance of Project class in multiple vm's, then use factories and some type of cache when constructing view models. If your vm reguires some more information, just create special Model class which will derive from Project (or implement IProject), so you can easilly use interface segregation principle.

提交回复
热议问题