Xamarin窗体的HttpClient PostAsync返回null

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

首页根据selected.Initially显示第一类产品的品类展示产品列表中。 API响应是空的第一个类别,但数量是正确的,只显示空白帧。与其他类别的开关后,产品被示correctly.Awaited的任务。我认为这个问题是异步等待calls.Help需要纠正这种..

        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>
c# xamarin xamarin.forms async-await
1个回答
1
投票

不透明度的方法Get_branch_products (string category_id) else块的内部设置为0.25。

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

如果里面加块未给它有解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.