Sending Generic Messages

对着背影说爱祢 提交于 2019-12-11 07:56:08

问题


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

public class Foo1
: Foo<Telephone>
{
}

public class Foo2
: Foo<Home>
{   
}

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

An example would be very much appreciated.


回答1:


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

Receiver code:

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

Sender Code:

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.

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

This is in my core module:

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



回答2:


An alternative is to create your own class that contains the payload you wish to deliver (Foo1 or simply object). Then in Foo2, register to receive messages of the type of the class you just created.

This link explains how with a very easy to understand example.

MVVM Light Toolkit Soup To Nuts 3 - Jesse Liberty



来源:https://stackoverflow.com/questions/6133152/sending-generic-messages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!