MVVM light - how to access property in other view model

前端 未结 3 561
情深已故
情深已故 2020-11-28 10:49

I\'m using mvvm light to build a Silverlight application. Is there a code snippet that shows how to access a view model\'s property or command from within another view model

3条回答
  •  时光取名叫无心
    2020-11-28 11:26

    You could use the Messenger to do this: Send the user in the UserViewModel:

    Messenger.Send(userInstance);
    

    would just send the user to anyone interested.

    And register a recipient in your CardViewModel:

    Messenger.Register(this, delegate(User curUser){_curUser = curUser;});
    

    or you can also send a request from your CardViewModel for shouting the user:

    Messenger.Send("Gimme user");
    

    And react on that in the UserViewModel:

    Messenger.Register(this, delegate(String msg)
    {
    if(msg == "Gimme user")
    Messenger.Send(userInstance);
    });
    

    (You better use an enum and not a string in a real scenario :) )

    Perhabs you can even response directly but I can't check it at the moment.

    Just check this out: Mvvm light Messenger

提交回复
热议问题