Xamarin表单等待异步任务

问题描述 投票:0回答:2

我有一个异步空白,我获取需要与本地数据进行比较的数据,然后填充listView。

CheckReservations(currentDate); //async void
BindingContext = viewModel = new ItemsViewModel(); //the model

但显然模型比async void更快地执行,有没有办法等待void完成然后填充模型?在获得HTTP响应之前我有await关键字,但它没有帮助,到目前为止,文档对我来说并不好。

我将代码更改为以下内容:

   protected async Task<ArrayList> CheckReservations(string day)
    {


        if (CrossConnectivity.Current.IsConnected)
        {
            try
            {

                var postData = new List<KeyValuePair<string, string>>();
                postData.Add(new KeyValuePair<string, string>("reservation_day", day));
                var content = new FormUrlEncodedContent(postData);
                var response = await _client.PostAsync(Url, content);

                if (response.IsSuccessStatusCode)
                {
                    var json = await response.Content.ReadAsStringAsync(); 
                    List<Reservations> myData = JsonConvert.DeserializeObject<List<Reservations>>(json);
                    foreach(Reservations res in myData)
                    {

                     reservations.Add(res.reservation_time);
                    }
                    return reservations;

                }
                else  { return null;  }
            }
            catch (Exception e)
            {
                Debug.WriteLine("" + e);
            }
        }
        return reservations ;
    }

并呼吁:

reservations =  (ArrayList) CheckReservations(currentDate);

但我得到错误:

Cannot convert type System.Threading.Tasks.Task<System.Collections.ArrayList> to System.Collections.ArrayList.

那么我做错了什么?

c# xamarin xamarin.forms xamarin.ios xamarin.android
2个回答
3
投票

我有一个异步空白,我获取需要与本地数据进行比较的数据,然后填充listView。

如果你自己做了这个异步无效,我建议你用一个Task来改变它,异步void是一个坏习惯,除非它是一个生命周期方法。

await CheckReservations(currentDate); //async Task
this.BindingContext = viewModel = new ItemsViewModel(); //the model

确保您的CheckReservations方法是一项任务,

   protected async Task<ArrayList> CheckReservations(string day)
{
    if (CrossConnectivity.Current.IsConnected)
    {
        try
        {
            var postData = new List<KeyValuePair<string, string>>();
            postData.Add(new KeyValuePair<string, string>("reservation_day", day));
            var content = new FormUrlEncodedContent(postData);
            var response = await _client.PostAsync(Url, content);

            if (response.IsSuccessStatusCode)
            {
                var json = await response.Content.ReadAsStringAsync(); 
                List<Reservations> myData = JsonConvert.DeserializeObject<List<Reservations>>(json);
                foreach(Reservations res in myData)
                {
                 reservations.Add(res.reservation_time);
                }
                return reservations;
            }
            else  { return new ArrayList();  }
        }
        catch (Exception e)
        {
            Debug.WriteLine("" + e);
            return new ArrayList();
        }
    }
    return reservations ;
}

1
投票

调用await方法时需要使用async关键字

reservations =  await CheckReservations(currentDate);
© www.soinside.com 2019 - 2024. All rights reserved.