Xamarin Forms执行从等待Httpclient PostAsync返回

时间:2019-02-04 13:25:09

标签: xamarin xamarin.forms

我正在viewmodel内部调用方法getaddress();,以从API获取地址信息。但是在调试时,执行返回      var response = await client.PostAsync(base_url + "api_delivery_details", formcontent).ConfigureAwait(false);到调用方法getaddress()中,但稍后执行其余部分。因此得到空结果。如何从头开始执行所有任务?

public async void Get_branch_products()
    {
        RestClient restClient = new RestClient();            
        getaddresses();  //this is the calling method

        long delivery_fee1 = long.Parse(delivery_fee);
        GrandTotal = Total + (Total * Tax) / 100 + delivery_fee1 ;            
        OnPropertyChanged("GrandTotal");
        tax_amt = (Total * Tax) / 100;
        OnPropertyChanged("tax_amt");
    }

public async void getaddresses()
    {
        ind_vis = true;
        OnPropertyChanged("ind_vis");             
        int user_id = (int)App.Current.Properties["user_id"];
        RestClient restClient = new RestClient();
        var result = await restClient.Get_address(user_id);  //calling restAPI method

        if (result.status == "success")
        {
            ind_vis = false;
            OnPropertyChanged("ind_vis");

public async Task<Address> Get_address(int user_id)
    {
        var formcontent = new FormUrlEncodedContent(new[]
        {
           new KeyValuePair<string,string>("user_id",user_id.ToString())
        });
  //returns from here// var response = await client.PostAsync(base_url + "api_delivery_details", formcontent).ConfigureAwait(false); 
  //executes later// var result = await response.Content.ReadAsStringAsync();
        var Items = JsonConvert.DeserializeObject<Address>(result);
        return Items;
    }

1 个答案:

答案 0 :(得分:2)

除非使用内置事件处理程序,否则在使用async-awaitasync voids创建后台线程时始终要记住一件事。

由于异步虚空会引起火灾,并且会忘记类型并引起很多难题。

您应该使用TaskTask<T>

我建议您在Async and Await Best Practices上阅读MSDN上的这本很棒的指南,如果您阅读的话,他们会很好地解释一切。

现在跳转到您的代码,您需要在代码中进行以下更改,以免发生此问题

  

执行在var response = await client.PostAsync(base_url + "api_delivery_details", formcontent).ConfigureAwait(false);返回到调用方法getaddress(),但稍后执行其余部分。因此得到空结果。如何从头开始执行所有任务?

按如下所示更改代码:

 public async Task Get_branch_products()
    {
        RestClient restClient = new RestClient();            
        await GetAddresses();  //this is the calling method

        long delivery_fee1 = long.Parse(delivery_fee);
        GrandTotal = Total + (Total * Tax) / 100 + delivery_fee1 ;            
        OnPropertyChanged("GrandTotal");
        tax_amt = (Total * Tax) / 100;
        OnPropertyChanged("tax_amt");
    }

public async Task GetAddresses()
    {
        ind_vis = true;
        OnPropertyChanged("ind_vis");             
        int user_id = (int)App.Current.Properties["user_id"];
        RestClient restClient = new RestClient();
        var result = await restClient.Get_address(user_id);  //calling restAPI method

        if (result.status == "success")
        {
            ind_vis = false;
            OnPropertyChanged("ind_vis");

public async Task<Address> Get_address(int user_id)
    {
        var formcontent = new FormUrlEncodedContent(new[]
        {
           new KeyValuePair<string,string>("user_id",user_id.ToString())
        });
  //returns from here// var response = await client.PostAsync(base_url + "api_delivery_details", formcontent).ConfigureAwait(false); 
  //executes later// var result = await response.Content.ReadAsStringAsync();
        var Items = JsonConvert.DeserializeObject<Address>(result);
        return Items;
    }

完成此操作后,您的代码将等待GetAddresses方法来获取您的数据,然后它将进一步移动。