使用ListView Windows 8商店App增量加载

时间:2014-10-19 15:18:51

标签: c# listview gridview data-binding windows-store-apps

我正在开发一个Windows 8商店应用程序。我已经实现了一个列表视图。 listview包含图像,文本块和复选框控件。我的listview从互联网上获取数据我已经用listview和binded数据进行了xml解析到listview。 Listview包含如此多的图像。所以我想在listview中实现增量加载。我在谷歌搜索了很多增量加载。但我无法从所有结果中理解。请帮我。对不起我的英语不好。 我的XAML:

    <ListView Name="display" ItemsSource="{Binding}"   SelectionMode="Single" 
 SelectionChanged="display_SelectionChanged" 
 ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.HorizontalScrollBarVisibility="Visible"   
 ItemContainerStyle="{StaticResource ListViewItemStyle12}" >
   <ListView.ItemsPanel>
      <ItemsPanelTemplate>
         <StackPanel x:Name="stak2" Orientation="Horizontal" />
      </ItemsPanelTemplate>
   </ListView.ItemsPanel>
    <ListView.ItemTemplate>
       <DataTemplate>
          <StackPanel Orientation="Vertical">
            <Image Source="{Binding  Path=Image}" Width="450" Tapped="image_taped" />
            <CheckBox Tag="{Binding Path=tag}" Visibility="{Binding Path=visichk}" Height="40"      Name="addremove" 
                HorizontalAlignment="Center" Checked="add_checked" Unchecked="sub_checked"  Opacity="0.5" 
                 Background="White" VerticalAlignment="Top" Template="{StaticResource CheckboxImageTemplate}" >
            </CheckBox>
            <TextBlock Text="{Binding Image_code}" FontSize="25" Foreground="Gray" HorizontalAlignment="Center"  />
          </StackPanel>
       </DataTemplate>
     </ListView.ItemTemplate>
 </ListView>

我的ProductData类

 public class ProductData
{

    public string Id { get; set; }
    public string Description { get; set; }
    public string categoryName { get; set; }
    public string productName { get; set; }
    public string Image { get; set; }
    public string Image_code { get; set; }
    public  bool IsSelected { get; set; }

}

我的代码

async void displayurl(string catId, string colorId, string accesskey)
    {
      try
        {
          var httpClient = new HttpClient(new HttpClientHandler());
          var values = new List<KeyValuePair<string, string>>
          {
            new KeyValuePair<string, string>("accesskey",accesskey),
            new KeyValuePair<string, string>("searchcategory",catId),
            new KeyValuePair<string, string>("colorcategory",colorId)
          };
         HttpResponseMessage response = await httpClient.PostAsync(wServiceProducts, new FormUrlEncodedContent(values));
         response.EnsureSuccessStatusCode();
         var responseString = await response.Content.ReadAsStringAsync();
         XDocument xmlDoc = XDocument.Parse(responseString);
         var Categories = xmlDoc.Descendants("product").ToArray();
         List<ProductData> displaylst = new List<ProductData>();
         foreach (var cat in Categories)
           {
              string prId = cat.Elements("id_products").Select(r => r.Value).FirstOrDefault();
               string productname = cat.Elements("productname").Select(r => r.Value).FirstOrDefault();
               string description = cat.Elements("description").Select(r => r.Value).FirstOrDefault();
               string categoryname = cat.Elements("categoryname").Select(r => r.Value).FirstOrDefault();
                bool selected = false;
                 List<string> Image = cat.Descendants("images").Elements("src").Attributes("largimage").Select(r => r.Value).ToList();
                 List<string> Image_code = cat.Descendants("images").Elements("src").Select(r => r.LastAttribute.Value).ToList();
                 int i = 0;
                foreach (string img in Image)
                 {
                   displaylst.Add(new ProductData {IsSelected=selected, Id = prId, categoryName = categoryname, productName = productname, Description = description, Image = img, Image_code = Image_code[i] });
                        i++;
                  }
            }
       display.ItemsSource = displaylst;
   }
   catch (Exception ex)
     {
       throw ex;
     }
 }

0 个答案:

没有答案