列表到 ObservableCollection

List To ObservableCollection

我正在写一个WindowsPhone8.1 App。它从服务器获取JSON并反序列化将其存储到列表中。我已将 ListView 绑定到此 List。现在,如果将新项目添加到 List,则不会更新 ListView。因此,我试图将此列表更改为 ObservableCollection。但我对互联网上的示例和教程感到困惑。

XAML:

 <DataTemplate 
            x:Key="StandardProductsItemTemplate">
            <Grid >
                <StackPanel 
                    Orientation="Vertical">
                      <Image 
                    Source="{Binding ProductImage}" 
                        x:Name="ImageDisplay"/>                        
                     <TextBlock 
                    Text="{Binding ProductName}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>

C#:

List<Product> ProductsObject = new List<Product>();

 for (int i = 0; i < GetProductByCategoryResultObject.Products.Count; i++)
            {
             ProductsObject.Add(GetProductByCategoryResultObject.Products[i]); 
            }
ProductListView.ItemsSource = ProductsObject;

现在,我想使用 ObservableCollection 代替 list 让 ListView 保持更新.请帮助

您可以简单地用 ObservableCollection 替换您的列表,示例中的语法保持不变:

ObservableCollection<Product> ProductsObject = new ObservableCollection<Product>();

 for (int i = 0; i < GetProductByCategoryResultObject.Products.Count; i++)
     {
      ProductsObject.Add(GetProductByCategoryResultObject.Products[i]); 
     }
ProductListView.ItemsSource = ProductsObject;
ObservableCollection<Product> ProductsObject = new ObservableCollection<Product>();

 for (int i = 0; i < GetProductByCategoryResultObject.Products.Count; i++)
            {
             ProductsObject.Add(GetProductByCategoryResultObject.Products[i]); 
            }
ProductListView.ItemsSource = ProductsObject;