MVVM : Share data between ViewModels

前端 未结 5 853
一个人的身影
一个人的身影 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:43

    You could have a static collection which your view model populate before you navigate to the new view model. The target view model can then retrieve the data from within it's constructor.

    For example ViewModel1 (VM1) will create a Project and populate it. VM1 will then put the Project into a shard, static, collection. VM1 will then navigate to another view model (VM2). In the constructor of VM2 you would go to the collection and retrieve the Project placed in there by VM1.

    If you used a dictionary of key-value pairs it would also allow you to share other data between view models.

提交回复
热议问题