如何从MessagingCenter调用中返回值?

时间:2015-01-09 16:09:16

标签: xamarin.forms

我们正在为我们的项目使用Xamarin.Forms,并在DisplayAlertDisplayActionSheet本机函数上订阅了MessagingCenter调用。

以下是我们在视图中订阅的方式:

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

以下是我们在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);
}

所以我们可以这样称呼它:

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

我们如何将值返回给邮件发件人?

1 个答案:

答案 0 :(得分:0)

您可以通过MessagingCenter发回DisplayActionSheet的结果。

例如

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