开发者

how to get answers from other Viewmodel

SL4 VS2010 MVVM-light c# ,messenger.

  1. I have a parent userControl calling a child userControl. (both have ViewModels)

  2. I am using messenger from MVVM-light.(the child is a usercontrol in a Tab of the parent)

  3. When the child needs to close we开发者_JAVA技巧 must check certain rules on the parent-ViewModel (the parent result is you can close on not).

How would you communicate the parent-ViewModel from the child-ViewModel?

message child to parent asking can I close? then, message the parent to child to return yes or no you may

(me? don't like to message twice back and forth)

what I really want to know is if there is any other way to preform communication like these among ViewModels?

or how about dimmed the close button on child if the parent rules don't allow the child to close.

Thanks for your time.


You should be able to use Messenger in MVVM-Light to achieve this task.

The child window publishes the messenger notification as shown below

var message = new NotificationMessageAction<bool>("CanClose", CloseCallback);
Messenger.Default.Send(message);

You can define the Action callback as shown below

private void CloseCallback(bool result)
{
    if (result)
        ... do some work and close, may need to use Dispatcher ...
}

You can subscribe for this notification on parent as shown below

Messenger.Default.Register<NotificationMessageAction<string>>(
   this,
   msg =>
      {
           if (msg.Notification == "CanClose")
               {
                   // Do the necessary UI logic and send the result back
                   msg.Execute(true);
               }
      }

Please let me know if this helps.


It's hard to say, but from what you've described, it sounds like you have some rules in the parent view model that belong in the model. If you pushed those rules into the model, both the parent and the child VMs could use those.

However, if that's not the case, you could always have some sort of publish/subscribe relationship that both the parent and the child register with and use those events to communicate.

I'd lean towards the model approach.


You have several options but the best answer is probably the one that fits the best in the rest of the overall design of your application. One option that hasn't been brought up is that your child-ViewModel could have a reference to the parent-ViewModel (this could have been provided within the original message that opened the child) and the child-ViewModel can ask the parent-ViewModel any question it needs via a method or property.

Example query from child-ViewModel:

bool canSave = _parentViewModel.CanSave(childInfo)

If you would like to use this child from several types of parents then you could create an interface which ensures implementers have a CanSave method that can always be called from the child.

interface ISaveQuerable
{
    bool CanSave(YourType childInfo);
}

This is just one option and it would work whether you are using messaging or direct ViewModel to ViewModel creation and communication. The main idea is that instead of creating callbacks that if you know the parent should always implement some functionality then providing that fact in the message enforces this as part of the message contract. Those who create the child using the message would have to provide something that implements the interface and the child would be guaranteed to have something available to call into in a strongly typed and compile time checked way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜