发送通用消息 [英] Sending Generic Messages

查看:83
本文介绍了发送通用消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Foo<T> where T: Entity
{}

public class Foo1
: Foo<Telephone>
{
}

public class Foo2
: Foo<Home>
{   
}

如何将Foo1发送到Foo2?我意识到该消息是键入的,因此收到的消息类型相同-但我需要在派生类之间进行消息...

How do I send Foo1 to Foo2? I realize that the message is typed, and hence messages of the same type of recieved - but I need to message between the derived classes...

一个例子将不胜感激.

推荐答案

从理论上讲,mvvmlight中的消息传递很容易被人遗忘……发件人不在乎谁收到消息,而收件者不在乎谁发送消息. ,只要它是所听的正确类型. 通过反复试验,我发现制作自己的消息比使用mvvm-light提供的默认值容易得多,它们是一个很好的起点,但是有时您会发现自己跳了圈.

the messaging in mvvmlight is in theory supposed to be fire and forget...the sender doesnt care who gets the message and the receiver doesnt care who sends the message, so long as its the right type that its listening for. I have found through much trial and error that its much easier to craft your own messages than use the default provided by mvvm-light, they are a good starting point, but sometimes you will just findyourself jumping through hoops..

public class ExceptionMessage : GalaSoft.MvvmLight.Messaging.GenericMessage<System.Exception>
    {
        public ExceptionMessage(System.Exception content) : base(content) { }
        public ExceptionMessage(object sender, System.Exception content) : base(sender, content) { }
        public ExceptionMessage(object sender, object target, System.Exception content) : base(sender, target, content) { }
    }

接收方代码:

Messenger.Default.Register<Core.Messaging.ExceptionMessage>(this, ex => ShowExceptionMessage(ex));

发件人代码:

public void LogException(Exception content)
        {
            _messenger.Send<Core.Messaging.ExceptionMessage>(new ExceptionMessage(content));
            //GetBw().RunWorkerAsync(content);
            WriteToDatabaseLog(content);
        }

是的,这确实违反了我的第一句话,但是从理论上讲,我可以有多个虚拟机或查看侦听异常消息.

and yes this does break the suggestion in my first sentence, but in theory I could have several vms or view listening for exception messages.

也许是另一个可以帮助您的例子...我讨厌整个foo事情...它总是让我感到困惑...

Maybe another example to help you out... i hate the whole foo thing...its always confuses me...

这在我的核心模块中:

public class SaveNotification<T> : GalaSoft.MvvmLight.Messaging.NotificationMessage<T> where T : GalaSoft.MvvmLight.ViewModelBase
    {
        public SaveNotification(T content, string notification) : base(content, notification) { }
        public SaveNotification(object sender, T content, string notification) : base(sender, content, notification) { }
        public SaveNotification(object sender, object target, T content, string notification) : base(sender, target, content, notification) { }
    }

这是我在虚拟机中使用它的方式:

here is how I used it in my vm:

public void OnSubmitChanges(SubmitOperation so)
        {
            if (so.HasError)
            {
                Infrastructure.GetService<IExceptionLoggingInterface>().LogException(this, so.Error);
            }
            else
            {
                //Save Responses
                _messenger.Send<Messages.NavigationRequest<SubClasses.URI.PageURI>>(GetNavRequest_HOME());
                ClearQuestionaire(true);
                _messenger.Send<WavelengthIS.Core.Messaging.SaveNotification<QuestionairreViewModel>>(GetSuccessfulSaveNotification());

            }

            Wait.End();
        }

        private WavelengthIS.Core.Messaging.SaveNotification<QuestionairreViewModel> GetSuccessfulSaveNotification()
        {
            return new WavelengthIS.Core.Messaging.SaveNotification<QuestionairreViewModel>(this, "Save Successfull");
        }

这篇关于发送通用消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆