Reputation: 4316
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?
Upvotes: 3
Views: 1806
Reputation: 891
You could send back the result of the DisplayActionSheet via the MessagingCenter.
For example
await result = DisplayActionSheet(....);
MessagingCenter.Send<MyPage, string> (this, "DisplayResult", result);
// Then where you need it
MessagingCenter.Subscribe<MyPage, string> (this, "DisplayResult", (sender, displayResultString) => {
// do something with displayResultString
});
Upvotes: 1