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>
Hi @JenalynParagados try this
`
public BuyProduct(GooglePurchase ggSearch=null)
{
_ggSearch = ggSearch;
InitializeComponent(); 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" }); NavigationPage.SetHasNavigationBar(this, false); var tapPurchase = new TapGestureRecognizer(); var tapBackBtn = new TapGestureRecognizer(); tapBackBtn.Tapped += OnTapBackBtnRecognizer; tapPurchase.Tapped += OnTapBuyProductRecognizer; }
`
Answers
Hi @JenalynParagados try this
`
public BuyProduct(GooglePurchase ggSearch=null)
{
_ggSearch = ggSearch;
`
Hi @PubuduGayan , Thanks. I just need to call this InitializeComponent(); before the implementation of the data binding