我从列表中以编程方式创建了一个网格。但是,我freshmvvm
也在使用,它给我推送新页面带来了一些麻烦。我注意到CoreMethods
是空的。这是我的课。
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++;
}
}
}
}
}
}
互联网并没有真正提供帮助。但是,周围有人说是因为设置了bindingcontext
. 但如果我不这样做,我将无法使用我的viewmodel
. 我该如何解决这个问题?