我们在项目中使用Xamarin.Forms,并订阅了对DisplayAlert和DisplayActionSheet原生函数的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);我们如何向消息发送者返回值?
发布于 2015-02-06 19:13:42
您可以通过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
});https://stackoverflow.com/questions/27864635
复制相似问题