Forum Xamarin Cross Platform with Xamarin

Data Binding Error

Hi, I wanted to use the data binding to add the products but then only errors will display saying 'Object Reference not set to an instance of an object".
Here are my codes:

ObservableCollection<InAppProduct> _products = new ObservableCollection<InAppProduct>();

public BuyProduct(GooglePurchase ggSearch=null)
        {
            _ggSearch = ggSearch;

            ProductView.ItemsSource = _products;

            _products.Add(new InAppProduct
            {
                SKU = "Small Monkey",
                Description = "Keyboard companion",
                Name = "monkey1.png"
            });
            _products.Add(new InAppProduct
            {
                SKU = "Small Monkey",
                Description = "Keyboard companion",
                Name = "monkey1.png"
            });
            _products.Add(new InAppProduct
            {
                SKU = "Small Monkey",
                Description = "Keyboard companion",
                Name = "monkey1.png"
            });

            InitializeComponent();

            NavigationPage.SetHasNavigationBar(this, false);

            var tapPurchase = new TapGestureRecognizer();
            var tapBackBtn = new TapGestureRecognizer();

            tapBackBtn.Tapped += OnTapBackBtnRecognizer;
            tapPurchase.Tapped += OnTapBuyProductRecognizer;
        }

xaml file:

<ListView x:Name="ProductView">
            <ListView.ItemTemplate>
              <DataTemplate>
                <TextCell Text="{Binding SKU}" />
              </DataTemplate>
            </ListView.ItemTemplate>
          </ListView>

Best Answer

Answers

  • JenalynParagadosJenalynParagados USMember ✭✭

    Hi @PubuduGayan , Thanks. I just need to call this InitializeComponent(); before the implementation of the data binding

Sign In or Register to comment.