【发布时间】:2016-10-02 18:14:53
【问题描述】:
我有一个async 函数:
public async Task<List<Data>> GetDataAsync()
{
//get data
return new List<Data>; //but then filled with real data
}
我这样称呼它:
listView.ItemsSource = await sc.GetSensorsAsync();
这样当函数完成获取数据时会弹出listView。
现在我想将相同的 List<Data> 保存到变量中,但不再调用 sc.GetSensorDataAsync()。
我试过了:
List<Data> data = await sc.GetSensorsAsync();
listView.ItemsSource = data;
但是因为函数是异步的,所以当data变量还是null时,它会执行listView.ItemsSource = data;。
如何解决这个问题,让listView.ItemsSource 和data 都包含返回值?
【问题讨论】:
-
But because the function is async, it will execute the listView.ItemsSource = data; when the data variable is still null在分配数据之前放置 await 怎么可能?也许方法返回 null?
标签: c# asynchronous xamarin async-await task-parallel-library