3

Is it possible to send a List in MVVM Light Message.

For example, I have a class named Authors. I want to send

Messenger.Default.Send(AuthorList); // AuthorList is of type List<Author>

in the constructor of the view model I am writing

Messenger.Default.Register<List<Author>>(this, authList => 
    {MessageBox.Show(authList[0].name)});

I have made sure that the constructor is called before I send the message. But it doesn't seem to work.

Pranjal
  • 796
  • 1
  • 8
  • 24

1 Answers1

3

Yes . Create your class (I'm using MyTest which has a simple string property in it):

public partial class MyTest
{
    public MyTest(string some_string)
    {
        S = some_string;
    }

    public string S { get; set; }
}

You can call it from wherever you want, in the ViewModel,I added a button that will create that list and send it to a different view.:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        var list_of_objects = new List<MyTest> { new MyTest("one"), new MyTest("two") };
        Messenger.Default.Send(list_of_objects );
    }

On the receiving ViewModel, add this in the constructor to register to that type of messages, and create a method that will be called when the message arrives:

// When a message with a list of MyTest is received
// this will call the ReceiveMessage method  :  
Messenger.Default.Register<List<MyTest>>(this, ReceiveMessage);

Implement the callback method:

private void ReceiveMessage(List<MyTest> list_of_objects)
    {
        // Do something with them ... i'm printing them for example         
        list_of_objects.ForEach(obj => Console.Out.WriteLine(obj.S));
    }

You're done :)

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • Thanks for your nice answer to the question. When I implement it, it complains about the RecieveMessage type and it says it should be Action type. Should I implement the CallBack Method in the ViewModel that receives the message? and what is wrong in my code: Messenger.Default.Register(_profileList , ReceiveMessage) ......................... Private sub ReceiveMessage(ProfileListt As List(of Profile)) _profileList =ProfileListt End sub ------------------------------------------- Thanks. (My code is in VB) – Ehsan Aug 14 '19 at 17:23
  • 1
    well, i'm not familiar with VB, and wow, it's been 6 years since i've answered this. Does VB `()` equals C# `<>`, because the above code registers a generic type. Sorry I can't give you a better answer. Maybe open a new one for your VB issue otherwise ? – Noctis Aug 15 '19 at 05:40
  • I think the syntax is correct, but for registering in the view model that receives the object, it does not let me put the ReceiveMessage function. It says it should be an Action! I thought it may have a simple answer. Thanks for replying anyway. – Ehsan Aug 15 '19 at 12:44
  • 1
    in c# an action is a method that doesn't return anything (vs a function, which returns something). so, maybe on your registration you need to simply pass the name of the function ? You'll have to search for it, but it sounds ok. Sorry i couldn't help more – Noctis Aug 16 '19 at 00:24