如何使消息目标成为目标消息的唯一接收者?

发布于 2024-09-24 05:43:58 字数 1200 浏览 2 评论 0原文

第一次海报。

我将 MVVM-Light 与 Silverlight 4 和 RIA 服务结合使用。这是一次学习经历!但到目前为止,它运行良好。。我想知道两件事。现在,我正在使用 Messenger 框架将 EntityObjects 传递回 ViewModel。例如,我需要打开一个具有特定“课程”对象的视图模型。因此,我实例化了视图,视图将消息与课程一起发送到 ViewModel。我有几个问题。

第一个问题:这是最好的方法吗?我不想使用 Prism 或 Unity 或任何其他东西,因为我没有时间学习它们。 (对我来说,这是 MVVM Light 的一大吸引力。Light 部分。)但是我看不到任何其他方法将参数传递给 VM 定位器。

第二部分是,这意味着我正在将消息从视图发送到该视图的特定 ViewModel。我的消息如下所示:

  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);  

因此,如您所见,我将 Course 和 DomainContext 捆绑在一起(啊 RIA。为什么不让我从 EntityObject 获取 Context?)并将它们发送到 ViewModel(这是“this.DataContext”) - 是的,我知道我应该为该消息创建一个类。

问题是 - 每个获得 Course 和 DomainContext 的对象都会收到该消息,而不仅仅是我指定目标的 VM。

那么,第二个问题:这是设计使然,还是一个错误,或者我做错了什么?

谢谢!

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 Light 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落在眉间の轻吻 2024-10-01 05:43:58

要回答第二个问题,如果您发送特定类型的NotificationMessage,则任何注册相同消息类型的人都将收到该消息。如果您想限制谁接收消息,请创建一个继承自 MessageBase 或 NotificationMessage 的新消息类,或者使用 令牌,或者在消息接收处理程序中使用 if 语句来过滤掉您不关心的消息。

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.

听,心雨的声音 2024-10-01 05:43:58

当您需要从一个 ViewModel 与另一个 ViewModel 进行通信,或者您需要发送一条消息,其中零到多个事物可以对其采取操作时,消息传递会更有用。从你的视图后面的代码来看,我认为你应该直接调用你的视图模型。这很简单 - 这就是我通常在代码中执行此操作的方式。

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
    }
}

我还在示例中介绍了如何通过事件处理从 ViewModel 返回到 View 的通信。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文