0

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

SecGen Unity
  • 35
  • 1
  • 5
  • Check this thread: https://stackoverflow.com/questions/38754635/jsonconvert-deserializeobject-case-sensitive. Probably `JsonConvert.DeserialzeObject` expects keys in your `Product.json` to be camel-case. – Quercus Sep 13 '22 at 06:20
  • I have read your question. The name attribute is in the prodcutsitem class so you may change it to this: Text = "{Binding ProdcutsItem[0]. name}". – Hongxin Sui-MSFT Sep 13 '22 at 07:23
  • You've created a fairly complicated situation. It would be wise to first get a simple case working. Add the complexity in steps. First, does `de` contain what you expect? Set breakpoint and examine. If it does, then this problem has nothing to do with json, and the title is misleading. If it doesn't, then all the rest of the logic is moot. Isolate the problem. – ToolmakerSteve Sep 13 '22 at 20:25

0 Answers0