开发者

How do I make Message targets be the only recipent of a targeted Message?

First time poster.

I'm using MVVM-Light with Silverlight 4 and RIA Services. This has been a learning experience! But so far, it's working beautifully. I was wondering two things. Right now, I'm using the Messenger framework to pass EntityObjects back to the ViewModel. For instance, I need to open a View Model with a specific "Course" object. So I instantiate the View, and the View sends a Message to the ViewModel with the Course. I've got a couple questions.

First question: Is this the best way to do this? I don't want to use Prism or Unity or any of those other things because I don't have the time to learn them. (This was, for me, the big draw of MVVM Light. The Li开发者_运维技巧ght part.) But I couldn't see any other way to pass parameters to the VM Locator.

The second part is, this means I am sending messages from the View to that View's specific ViewModel. My messages look like this:

  Tuple<Models.Course, Services.VWDS> courseDomainContextTuple = new Tuple<Models.Course, Services.VWDS>(Course, DomainContext);

  NotificationMessage<Tuple<Models.Course, Services.VWDS>> message = new NotificationMessage<Tuple<Models.Course, Services.VWDS>>(this, this.DataContext, courseDomainContextTuple, Models.MessageString.EditCourse);

  Messenger.Default.Send<NotificationMessage<Tuple<Models.Course, Services.VWDS>>>(message);  

So, as you can see, I'm bundling the Course and the DomainContext (Ah RIA. Why won't you let me get the Context from the EntityObject?) and sending them to the ViewModel (which is "this.DataContext") - and yes, I know I should make a class for that message.

Here's the problem - every object that gets a Course and a DomainContext receives that message, not just the VM that I've designated the Target.

So, second question: Is that by design, or is that a bug, or am I doing something wrong?

Thanks!


To answer your second question, if you're sending a NotificationMessage of a specific type, anything registering for that same message type will receive the message. If you want to limit who receives the message, either create a new message class inheriting from MessageBase or NotificationMessage or whatever, send your message with a Token, or have an if statement in your message receive handler to filter out messages you don't care about.


Messaging is more useful when you need to communicate from one ViewModel to another, or you need to send a message where zero to many things can take action on it. From your View's code behind, I think you should just call your ViewModel directly. Its easy enough - here's how I usually do it in my code.

public partial class ExampleView : UserControl
{
    private IExampleViewModel ViewModel
    {
        get { return this.DataContext as IExampleViewModel; }
    }

    public ExampleView()
    {
        InitializeComponent();

        // Call directly to my View Model
        ViewModel.SomeMethod();

        // Register for View Model's event
        ViewModel.SomeEvent += ViewModel_SomeEvent;
    }

    private void ViewModel_SomeEvent(object sender, EventArgs e)
    {
        // do stuff
    }
}

I also included in the example how I handle communications from the ViewModel back to the View - through events.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜