简体   繁体   English

Xamarin Forms Httpclient PostAsync返回null

[英]Xamarin Forms Httpclient PostAsync returns null

Homepage showing products in a list according to the category selected.Initially showing products of first category. 根据所选类别在列表中显示产品的首页。最初显示第一类产品。 API response is null for first category but count is correct,showing only blank frames. 第一类的API响应为空,但计数正确,仅显示空白帧。 After switching from other categories, products are shown correctly.Awaited the tasks. 从其他类别切换后,产品将正确显示。等待任务。 I think the problem is with async-await calls.Help need to rectify this.. 我认为问题出在async-await呼叫上。需要纠正此问题。

        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>

opacity was provided inside the Get_branch_products (string category_id) method else block as 0.25. Get_branch_products (string category_id)方法内部提供的不透明度为0.25。

val.opacity = 1; 
OnPropertyChanged("opacity"); 

was not given inside if block adding it there solved the issue. 如果在其中添加块解决了问题,则不会在内部给出。

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

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