Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

How do you return a value from a MessagingCenter call?

$
0
0

We are using Xamarin.Forms for our project and subscribed a MessagingCenter call onto the DisplayAlert and DisplayActionSheet native functions.

Here's how we subscribed it in our view:

MessagingCenter.Subscribe<ViewModelBase, List<string>> (this, "DisplayActionSheet", (sender, values) => {
    string title = values[0];
    values.RemoveAt(0);
    DisplayActionSheet (title, "Annuler", null, values.ToArray());
});

Here's how we implemented it in our ViewModel:

public async void DisplayActionSheet(string title, string[] actions){
    List<string> values = new List<string>(actions);
    values.Insert (0, title);
    MessagingCenter.Send<ViewModelBase, List<string>> (this, "DisplayActionSheet", values);
}

So we can call it this way:

string[] actions = {"Charmander", "Pikachu", "Squirtle"};
DisplayActionSheet("Choose your pokemon", actions);

How can we return a value to the message sender?


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>