0

我从列表中以编程方式创建了一个网格。但是,我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. 我该如何解决这个问题?

4

1 回答 1

0

根据FreshMvvm的 Convention over Configuration 机制,您需要从 FreshBaseContentPage 继承 InventoryPage 而不是 ContentPage。

注意:您需要遵守该机制,即我们的页面名称(AKA 视图)必须以“Page”结尾,而我们的 pageModels(AKA 视图模型)名称必须以“PageModel”结尾,例如:Page - ------> 我的页面页面模型 --> 我的页面模型

通过遵循该规则,您的 BindingContext 将自动正确设置。

于 2018-02-28T14:06:14.317 回答