How To Load Json File into CollectionView or ListView In .Net Maui

Viewed 48

Hi I've a problem here

This is my Code

ProductViewModel.cs

public partial  class ProductVM : ObservableObject
{
    [ObservableProperty]

    ObservableCollection<ProductModel.ProductList> productList = new();


   
    
    [RelayCommand]
    
    async void  LoadProducts()
    {
       ProductModel.ProductList de = JsonConvert.DeserializeObject<ProductModel.ProductList>(File.ReadAllText("D:\\Products.json"));

        productList.Add(de);

       
       
        await Shell.Current.DisplayAlert("", ""+ productList[0].Products.Count, "Ok" );

      
    }

}

}

My ProductModel

public class ProductModel

{

public class ProductList
{
    public ObservableCollection<ProdcutsItem>  Products { get; set; }
}

public class ProdcutsItem
{
    public string name { get; set; }
  
    public int Price { get; set; }
}

}

And Here is Products.json File

{

"Products":[

{
    "Name":"test",
    "Price":0
},
{
    "Name":"Product 2",
    "Price":1000
},
{
    "Name":"Product ",
    "Price":1000
}

] }

And This is My View :

 <CollectionView  x:Name="cF"   BackgroundColor="Bisque" ItemsSource="{Binding ProductList[0].Products}">
        <CollectionView.ItemTemplate>
            <DataTemplate >
                <Grid>
                    <Label x:DataType="{Binding ProductList[0].Products}"  Text="{Binding ProductList[0].Products[0].name}"/>
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

Simply i want to load this Products.json to collectionview but 1: it displays nothing 2: I Can Not access inner ProductItems

0 Answers
Related