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

标签 xamarin.forms

我们在项目中使用 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/

相关文章:

c# - 使用 Xamarin 处理权限

android - Visual Studio for Mac 无法部署到兼容的 Android 模拟器

visual-studio - 预览 Xamarin 表单页面

mvvm - Xamarin Forms MVVM (Prism) with Media.Plugin - 如何从设备存储中获取照片

c# - 如何更改菜单栏中的 Xamarin 后退按钮?

xaml - XAML 编译在运行时的工作方式

c# - Xamarin Forms - 获取图像以填充包含网格列的宽度?

android - 您可以在运行时注册 url Intent 吗?

android - 点击当前选项卡刷新页面时的 Xamarin.Forms TabbedPage 事件

wpf - Azure 客户端 ID、租户和 Key Vault URI 是否被视为 secret ?