我试图创建一个按钮网格,每行显示5个按钮。我尝试使用nuget Xamarin.Forms.Datagrid。但它并没有真正起作用。我在部分单声道代码中获得空引用说错误。数据网格的工作方式对我来说很难理解。这是我的代码。
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CashRegisterApp.Pages.InventoryPage"
xmlns:dg="clr-namespace:Xamarin.Forms.DataGrid;assembly=Xamarin.Forms.DataGrid">
<ContentPage.Content>
<ScrollView>
<dg:DataGrid ItemsSource="{Binding Buttons}" SelectionEnabled="True" RowHeight="250" Columns="5">
<dg:DataGrid></dg:DataGrid>
</dg:DataGrid>
</ScrollView>
</ContentPage.Content>
</ContentPage>
类
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using CashRegisterApp.Annotations;
using CashRegisterApp.Pages.Components;
using FreshMvvm;
using Xamarin.Forms;
namespace CashRegisterApp.ViewModels
{
public class InventoryViewModel : FreshBasePageModel, INotifyPropertyChanged
{
private List<Button> _buttons;
public List<Button> Buttons
{
get { return _buttons; }
set { _buttons = value; OnPropertyChanged(nameof(Buttons)); }
}
public InventoryViewModel()
{
Buttons = LoadInventory();
}
public List<Button> LoadInventory()
{
var buttons = new List<Button>();
buttons.Add(new AddFolderButton());
buttons.Add(new AddItemButton());
return buttons;
}
public void OnItemSelect()
{
}
public new event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}