【问题标题】:Xamarin Forms - Loading Picker control with async data on load of pageXamarin Forms - 在页面加载时使用异步数据加载 Picker 控件
【发布时间】:2017-09-27 14:27:11
【问题描述】:

我有一个 Xamarin 应用程序,我最近使用 Xamarin 将它包含到 Web 服务中,与 wcf Web 服务一起使用,并且工作正常 (http://developer.xamarin.com/guides/cross-platform/xamarin-forms/web-services/consuming/wcf/)

有一个 ListView 教程,这可以很好地使用控制器中的命令调用异步 Web 服务,并将其绑定到 Listview。

那么对于我的问题,当表单加载时,将异步 Web 服务数据加载到 xamarin 选择器控件中的最佳方法是什么?

我尝试在视图模型构造函数中做一些事情,但是控制数据在数据返回之前呈现......

public class UserPreferencesViewModel : BaseViewModel
  {...

public UserPreferencesViewModel()
    {
       LoadDropdownData();
      //carries on while data is being queried
    }

   async Task LoadDropdownData()
    {

        Services = new ObservableRangeCollection<Service>();
        var services = await App.ServiceManager.GetServiceDataAsync();
        Services.ReplaceRange(services);
    }

而且我似乎无法使用命令对象,因为没有 RefreshCommand 属性。

我还有需要将选择器值设置为的偏好数据,并且想知道如何最好地做到这一点。

希望这是有道理的

【问题讨论】:

  • 要么正确使用异步等待......所以在你的构造函数中:等待 LoadDropdownData 或使用 ObservableCollections 如果你的数据来自 WCF 服务的异步将填充 ListView
  • 你不能在构造函数中使用'await',所以 ObservableCollections 看起来有前途吗?我已经在使用 ObservableRangeCollections 作为 Service 类型。

标签: asynchronous xamarin xamarin.forms picker


【解决方案1】:

将 xamarin 选择器分配给可观察的集合,这很有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多