i am trying to load data from api but not displaying in view

i am trying to get data from api and loading in a view using INotifyPropertyChanged.

Data is coming from api in async request i can see in logs. meanwhile data in the ui is loading with out data .

here is my code

Xaml page

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="ShoppingCart.Views.MyCartPage">
    <ContentPage.Content>


    <StackLayout Margin="8"  >
    <ScrollView  >

        <ListView ItemsSource="{Binding Items}"
                  CachingStrategy="RecycleElement"
                  RowHeight="120" x:Name="CartListView" >

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Margin="8" Orientation="Horizontal" >

                            <Image Source="{Binding thumb}" />


                            <StackLayout Margin="8"  >

                            <Label Text="{Binding name}"
                                   FontAttributes="Bold" />

                            <Label Text="{Binding price}"  />
                            </StackLayout>

                        </StackLayout>
                    </ViewCell>

    <!--                <ImageCell ImageSource="{Binding image}" Text="{Binding name}"/>
    -->                    
                </DataTemplate>
            </ListView.ItemTemplate>

        </ListView>

        <StackLayout Margin="8" Orientation="Horizontal" >
            <Label Text="Total" FontAttributes="Bold" />

            <Label Text="{Binding total}"  />

        </StackLayout>


    </ScrollView  >

   </StackLayout>


    </ContentPage.Content>
</ContentPage>

Code behind

using System;
using System.Collections.Generic;
using System.Diagnostics;

using Xamarin.Forms;

using ShoppingCart.ViewModels;

namespace ShoppingCart.Views
{
public partial class MyCartPage : ContentPage
{
public MyCartPage()
{
InitializeComponent();

        MyCartViewModel vm = new MyCartViewModel();
        BindingContext = vm;

        //Debug.WriteLine("cartValue {0}", vm.Cart);
        //Debug.WriteLine("cartValue {0}", vm.Cart.data.total);

        //Debug.WriteLine("data loaded ");


    }
}

}

view model

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Diagnostics;

using Xamarin.Forms;

using ShoppingCart.Model;

namespace ShoppingCart.ViewModels
{
public class MyCartViewModel : INotifyPropertyChanged
{

    private Cart _cart;
    public Cart Cart{
        get { return _cart; }

        set {

            _cart = value;
            OnPropertyChanged("Cart");
        }
    }

    private ObservableCollection<CartProducts> items;
    public ObservableCollection<CartProducts> Items
    {
        get { return items; }
        set
        {
            items = value;
            OnPropertyChanged("Items");
        }
    }




    public MyCartViewModel()
    {
        Items = new ObservableCollection<CartProducts>();

        Cart = new Cart();
        this.GetCart();
    }


    private async void GetCart()
    {
        try
        {
            Cart = await App.ApiManager.GetCart();

            Debug.WriteLine("Cart {0}", Cart);

            foreach (var product in Cart.data.products)
            {
                Debug.WriteLine("product {0}", product.name);
                //Items.Add(new Products() { name =  product.name, image = product.image });
                Items.Add(product);
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
        }

    }

// private ObservableCollection cartProductsValue;
//public ObservableCollection CartProducts
//{

    //  get { return cartProductsValue; }
    //  set
    //  {
    //      cartProductsValue = value;
    //      OnPropertyChanged("CartProducts");
    //  }
    //}

// public MyCartViewModel()
// {
// CartProducts = new ObservableCollection();
// this.GetCart();
//}

    //private async void GetCart()
    //{
    //  try
    //  {
    //      cartValue = await App.ApiManager.GetCart();

// //CartProducts = Cartvals.data.products;

    //      foreach (var product in cartValue.data.products)
    //      {
    //          Debug.WriteLine("product {0}", product.name);
    //          //Items.Add(new Products() { name =  product.name, image = product.image });
    //          CartProducts.Add(product);
    //      }
    //  }
    //  catch (Exception e)
    //  {
    //      Debug.WriteLine(e.Message);
    //  }

    //}



    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));



}

}

Best Answers

Answers

Sign In or Register to comment.