开发者

MVVM: Using a Messenger only with custom objects to make Send<> "id" unique?

开发者 https://www.devze.com 2023-01-23 01:48 出处:网络
what I do not like about the Messenger I use (mvvm light toolkit) that when I register to DateTime and I send something other parts of my applica开发者_开发问答tion get the datatime data too because t

what I do not like about the Messenger I use (mvvm light toolkit) that when I register to DateTime and I send something other parts of my applica开发者_开发问答tion get the datatime data too because they registered to "DateTime" type.

To prevent that I have to create always custom objects and wrap my datetime value. Thats stupi.

How do you workaround that problem?


Sending messages of type DateTime seems like a bad idea to me... It's not specific enough, it doesn't carry any meaning.

Imagine you receive a letter that just contains a date, and nothing else: what would you make of it ? Is it a birthday, the release date of a movie, the deadline for paying your phone bill?

A date (or string, or int, or any primitive type) means nothing without context. You need to create specific message types, it's the only way the information you send can be meaningful.


You could send your message with a token object and then register only for DateTime sent with that particular token.

I seem to recall Laruent creating a class something like this:

public class Notifications
{
        public static readonly String SystemDateChanged = Guid.NewGuid().ToString();
        public static readonly String SomeOtherDateChanged = Guid.NewGuid().ToString();
}

Then send your message

Messenger.Default.Send(newSystemDateTime, Notifications.SystemDateChanged );

Parties interested in the SystemDateTime (and not other date times) would register for it

Messenger.Default.Register<DateTime>(this, Notifications.SystemDateChanged, true,
d => { mydate = d; });
0

精彩评论

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

关注公众号