0

I created a grid programmatically from a list. However, I'm using freshmvvm also and it is giving me some troubles with pushing a new page. I notice that the CoreMethods is null. this is my class.

using System;
using CashRegisterApp.ViewModels;
using Xamarin.Forms;

namespace CashRegisterApp.Pages
{
    //[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class InventoryPage : ContentPage
    {
        public InventoryPage()
        {
            InitializeComponent();
            BindingContext = new InventoryViewModel();
            CreateGrid();
        }

        /// <summary>
        /// Creates a grid specifically with the needed columns and rows for the list in the viewmodel
        /// </summary>
        private void CreateGrid()
        {
            //var grdInventory = new Grid();

            if (BindingContext is InventoryViewModel vm)
            {
                var buttonList = vm.Buttons;

                //determine the amount of rows needed to create the full grid of buttons
                var x = 5;
                decimal rowCount = buttonList.Count / x;
                var y = Math.Round(rowCount, MidpointRounding.AwayFromZero);
                if (y == 0)
                {
                    y = 1;
                }
                //declare the rows and the columns

                for (int i = 0; i != x; i++)
                {
                    grdInventory.ColumnDefinitions.Add(new ColumnDefinition{ Width = new GridLength(1, GridUnitType.Star)});
                }

                for (int i = 0; i != y; i++)
                {
                    grdInventory.RowDefinitions.Add(new RowDefinition{ Height = new GridLength(220)});
                }

                //fill in the grid using for loops atm cus it is the solution i know
                var count = 0;
                for (int i = 0; i != y && count != buttonList.Count; i++)
                {
                    for (int j = 0; j != x && count != buttonList.Count; j++)
                    {
                        grdInventory.Children.Add(buttonList[count], j, i);
                        count++;
                    }
                }
            }
        }    
    }
}

The internet has not really been helpful. however, reading around someone said its because of setting the bindingcontext. But if I don't do that I cannot use the list from my viewmodel. how can I resolve this?

FaizanHussainRabbani
  • 2,826
  • 2
  • 19
  • 41
Bjorn Fontaine
  • 345
  • 3
  • 14
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Camilo Terevinto Feb 28 '18 at 13:51
  • How are you navigating to this page? And why are you setting the `BindingContext` yourself? If there's one thing FreshMvvm does, it's pagemodel to pagemodel navigation. I think you should check the way you're navigating to this page and do it via FreshMvvm to get everything set up right – Gerald Versluis Feb 28 '18 at 13:51

1 Answers1

0

According to FreshMvvm's Convention over Configuration mechanism, you need to inherit InventoryPage from FreshBaseContentPage instead of ContentPage.

Note: You need to adhere to that mechanism, i.e the name of our pages(AKA view) has to end with "Page" and the name of our pageModels(AKA viewModel) has to end with "PageModel", for example: Page -------> MyPage PageModel --> MyPageModel

By following that rule, your BindingContext will be set correctly automatically.

VahidShir
  • 1,718
  • 2
  • 13
  • 24
  • i used the correct naming convention for the freshmvvm stuff. (ViewModel is also allowed). but i did change my inherit class to the one u mentioned. however i'm still getting the same error and CoreMethods is still null. – Bjorn Fontaine Feb 28 '18 at 14:45