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?