Xamarin Forms Httpclient PostAsync返回null

时间:2019-02-05 11:07:43

标签: c# xamarin xamarin.forms async-await

根据所选类别在列表中显示产品的首页。最初显示第一类产品。第一类的API响应为空,但计数正确,仅显示空白帧。从其他类别切换后,产品将正确显示。等待任务。我认为问题出在异步等待调用上。需要帮助纠正这一点。

        private async Task InitializeAsync()
        {
            await Get_categories(); //calling method
        }
        public Task Initialization { get; private set; }       

        public  Grid_vm() //constructor
        {
            Initialization = InitializeAsync();
        }

        public async Task Get_categories()
        {                
            var items = await restClient.Get_categories();                           
            Categorylist = new List<Category_value>();                    
                foreach (Category_data cat in items.data)
                {
                    Category_value category = new Category_value();
                    if (cat.category_id=="6")
                    {
                        category.category_id = cat.category_id;
                        category.category_name = cat.category_name;                            
                        Categorylist.Add(category);    
                    }
                    else
                    {
                        category.category_id = cat.category_id;
                        category.category_name = cat.category_name;                            
                        Categorylist.Add(category);    
                    }
                }
                await Get_branch_products("6") ;  // calling first category                
        }

        public async Task Get_branch_products (string category_id)
        {                             
            RestClient restClient = new RestClient();
            var items=await restClient.Get_branch_products2(category_id);//calling restAPI                               
            Productlist = new List<Product_Value2>();//var items null initially
                    foreach (Product_Data p in items.data)
                    {
                        Product_Value2 val = new Product_Value2();
                        val.available_qty = p.available_qty;                            
                        if (p.status == "1")
                        {        
                             val.visible = true ;
                             OnPropertyChanged("visible");
                             val.visibility = false;
                             OnPropertyChanged("visibility");                                  
                             Productlist.Add(val);
                        }
                        else
                        {    
                             val.visible = true ;
                             OnPropertyChanged("visible");
                             val.visibility = false;
                             OnPropertyChanged("visibility"); 
                             val.opacity = 0.25;
                             OnPropertyChanged("opacity");                           
                             Productlist.Add(val);
                        }
                    }               
            }
        }

        public async Task<ProductDetails> Get_branch_products2(string cat_id)
                {                
                           var formcontent = new FormUrlEncodedContent(new[]
                            {
                                new KeyValuePair<string,string>("category_id",cat_id)
                            });    
      var response = await client.PostAsync(base_url + "listbranchproduct", formcontent).ConfigureAwait(false);//execution returns null from here
      var result = await response.Content.ReadAsStringAsync();//executed later  
                            var status = JsonConvert.DeserializeObject<ProductDetails>(result);
                            return status;                               
                }

<ListView  x:Name="pdt_list" HasUnevenRows="True" SeparatorVisibility="None" ItemsSource="{Binding Productlist}" BackgroundColor="White" Margin="0,0,0,0">
                    <ListView.ItemTemplate>
                        <DataTemplate >
                            <ViewCell >
                                <ViewCell.View>
                                    <Frame HasShadow="False" Margin=" 0,10,0,10"  Padding="10,0,10,5" BackgroundColor="#f1f1f1" HeightRequest="80" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" >
                                        <Grid Padding="0">
                                            <!--<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand"  Margin="0,10,0,10"  >-->
                                            <StackLayout VerticalOptions="FillAndExpand" Margin="0" Padding="10,0,0,0" Orientation="Horizontal" Opacity="{Binding opacity}">
                                                <Image Source="{Binding image}"  Aspect="AspectFill"  WidthRequest="70" HeightRequest="180"  VerticalOptions="FillAndExpand" />
                                                <StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical"  >
                                                    <Label Text="{Binding Name}" Font="Bold" VerticalTextAlignment="Center" FontSize="Small" TextColor="Black" FontFamily="opensans_light.ttf#opensans_light" Margin="10,20,0,0" />
                                                    <StackLayout Orientation="Horizontal" Margin="10,0,0,0" HorizontalOptions="Start" VerticalOptions="Start"  >
                                                        <Label Text="{Binding sellingprice_default}" Font="Bold"  HorizontalOptions="Start" Margin="0" TextColor="#FA2E27" VerticalTextAlignment="Start" FontSize="Small" FontFamily="opensans_light.ttf#opensans_light" />
                                                        <Label Text="QAR"  TextColor="#FA2E27" HeightRequest="90" FontSize="Small" HorizontalOptions="Start" VerticalTextAlignment="Start" VerticalOptions="FillAndExpand" />
                                                        </StackLayout>
                                                        </StackLayout>
                                                        <ImageButton Source="carts.png" BackgroundColor="Transparent" IsVisible="{Binding visible}" HorizontalOptions="EndAndExpand" WidthRequest="40" HeightRequest="40" VerticalOptions="CenterAndExpand"  Clicked="Add_cart_Clicked" Margin="0,20,0,0" Aspect="AspectFit">
                                                        </ImageButton>
                                                    <StackLayout Orientation="Horizontal" BackgroundColor="Transparent" IsVisible="{Binding visibility}" HorizontalOptions="EndAndExpand">
                                                        <ImageButton Source="minus.png" HorizontalOptions="End" VerticalOptions="Center"   Aspect="Fill"   BackgroundColor="Transparent"  WidthRequest="20" HeightRequest="20" Clicked="Minus_Tapped" />
                                                        <Label Text="{Binding Number2}" VerticalTextAlignment="Center" HorizontalOptions="EndAndExpand"   FontSize="Medium" TextColor="Black" FontFamily="opensans_light.ttf#opensans_light" Margin="0,0,0,0" />
                                                        <ImageButton Source="plus2.png" HorizontalOptions="End" Aspect="Fill" VerticalOptions="Center"  BackgroundColor="Transparent"   WidthRequest="20" HeightRequest="20"  Clicked="Plus_Tapped"   />
                                                    </StackLayout>
                                              </StackLayout>
                                             <StackLayout BackgroundColor="Black" HorizontalOptions="EndAndExpand" VerticalOptions="StartAndExpand" WidthRequest="100" HeightRequest="25" IsVisible="{Binding opaque}" Margin="0,0,0,0" >

                                                <Label Text="Not Available" FontFamily="opensans_light.ttf#opensans_light" TextColor="White" FontAttributes="Bold" HorizontalOptions="Center" VerticalTextAlignment="Center" />
                                            </StackLayout>
                                        </Grid>
                                    </Frame>
                                </ViewCell.View>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

1 个答案:

答案 0 :(得分:1)

Get_branch_products (string category_id)方法的else块中提供的

opacity为0.25。

val.opacity = 1; 
OnPropertyChanged("opacity"); 
如果在其中添加块解决了问题,则不会在其中提供