我们在项目中使用 Xamarin.Forms,并订阅了对 DisplayAlert
和 DisplayActionSheet
native 函数的 MessagingCenter 调用。
这是我们在 View 中订阅它的方式:
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);
我们如何向消息发送者返回一个值?
最佳答案
您可以通过 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
});
关于xamarin.forms - 如何从 MessagingCenter 调用返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27864635/