我们正在为我们的项目使用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);
我们如何将值返回给邮件发件人?
答案 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
});