Search code examples
c#xamarin.formsxamarin.androidmessagingcenter

How do I send a list of objects through MessagingCenter in C#?


I am trying to send list of BarData objects to my FavoritesPage.xaml.cs through MessagingCenter. I have tried

MessagingCenter.Send<BarData>(_favoriteBarsList, "FaveBars");

and it gives me an error, telling me I can't convert the sender from a list to an object. I then tried to use

MessagingCenter.Send<List<BarData>>(_favoriteBarsList, "FaveBars");

and visual studio screams at me LOL! I tried search online how to send a list of objects through MessagingCenter but I can't find anything. Can someone please help?


Solution

  • MessagingCenter.Send Method

    Sends a named message with the specified arguments

    Parameters

    • sender TSender

      • The instance that is sending the message. Typically, this is specified with the this keyword used within the sending object.
    • message String

      • The message that will be sent to objects that are listening for the message from instances of type TSender.
    • args TArgs

      • The arguments that will be passed to the listener's callback.

    Example

    MessagingCenter.Send<MainPage,List<BarData>>(
        this, // the context you are on 
        "FaveBars", // the named message
        _favoriteBarsList); // the argument
    

    Where MainPage is the type of this, and List<BarData> is the argument type