简体   繁体   English

Xamarin Form等待异步任务

[英]Xamarin Form waiting for async task

I have an async void where I get the data I need to compare with local data then populate the listView. 我有一个异步void,在这里我需要与本地数据进行比较的数据,然后填充listView。

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

But obviously the model gets executed sooner than the async void, is there any way to wait for the void to finish and then populate the model? 但是显然模型比异步void更快地执行,有什么方法可以等待void完成然后填充模型吗? I have the await keyword before getting the HTTP response, but it is not helping, Documentation didn't do well for me thus far. 在获取HTTP响应之前,我有await关键字,但是它没有帮助,到目前为止,文档对我来说还做得不好。

I changed my code to the following: 我将代码更改为以下内容:

   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 ;
    }

And the call to: 并致电:

reservations =  (ArrayList) CheckReservations(currentDate);

But I get the error: 但是我得到了错误:

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

So what am I doing wrong? 那我在做什么错?

I have an async void where I get the data I need to compare with local data then populate the listView. 我有一个异步void,在这里我需要与本地数据进行比较的数据,然后填充listView。

If you have made this async void yourself I would suggest you change it with a Task instead, an async void is a bad practice, unless it's a lifecycle method. 如果您自己使异步无效,建议您改为使用Task进行更改,异步无效是一种不好的做法,除非它是生命周期方法。

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

See to it that your CheckReservations method is a task, 确认您的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 ;
}

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

reservations =  await CheckReservations(currentDate);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM