gpt4 book ai didi

xamarin.forms - 如何从 MessagingCenter 调用返回值?

转载 作者:行者123 更新时间:2023-12-05 06:44:43 26 4
gpt4 key购买 nike

我们在项目中使用 Xamarin.Forms,并订阅了对 DisplayAlertDisplayActionSheet 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com