【发布时间】: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