【发布时间】:2020-04-02 08:31:14
【问题描述】:
我正在尝试在一个网格中制作 9 个方形按钮(我目前所拥有的图片如下)。我在 Visual Studio 2019 中使用 Xamarin Forms。似乎无论我做什么,我总是得到 8 个方形按钮和 1 个矩形按钮。有什么建议吗?
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Chess_test13.MainPage">
<StackLayout>
<Grid Padding="50,50" RowSpacing="0" ColumnSpacing="0" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button x:Name="00" ClassId="00" PropertyChanged="PropertyChanged" Text="" Grid.Row="0" Grid.Column="0" BackgroundColor="Transparent" Clicked="buttonClick" VerticalOptions="CenterAndExpand" HorizontalOptions="Fill" HeightRequest="{Binding Width,Source= {x:Reference 00}}" />
<Button x:Name="01" ClassId="01" PropertyChanged="PropertyChanged" Text="" Grid.Row="0" Grid.Column="1" BackgroundColor="Transparent" Clicked="buttonClick" VerticalOptions="CenterAndExpand" HorizontalOptions="Fill" HeightRequest="{Binding Width,Source= {x:Reference 01}}" />
<Button x:Name="02" ClassId="02" PropertyChanged="PropertyChanged" Text="" Grid.Row="0" Grid.Column="2" BackgroundColor="Transparent" Clicked="buttonClick" VerticalOptions="CenterAndExpand" HorizontalOptions="Fill" HeightRequest="{Binding Width,Source= {x:Reference 02}}" />
<Button x:Name="10" ClassId="10" PropertyChanged="PropertyChanged" Text="" Grid.Row="1" Grid.Column="0" BackgroundColor="Transparent" Clicked="buttonClick" VerticalOptions="CenterAndExpand" HorizontalOptions="Fill" HeightRequest="{Binding Width,Source= {x:Reference 10}}" />
<Button x:Name="11" ClassId="11" PropertyChanged="PropertyChanged" Text="" Grid.Row="1" Grid.Column="1" BackgroundColor="Transparent" Clicked="buttonClick" VerticalOptions="CenterAndExpand" HorizontalOptions="Fill" HeightRequest="{Binding Width,Source= {x:Reference 11}}" />
<Button x:Name="12" ClassId="12" PropertyChanged="PropertyChanged" Text="" Grid.Row="1" Grid.Column="2" BackgroundColor="Transparent" Clicked="buttonClick" VerticalOptions="CenterAndExpand" HorizontalOptions="Fill" HeightRequest="{Binding Width,Source= {x:Reference 12}}" />
<Button x:Name="20" ClassId="20" PropertyChanged="PropertyChanged" Text="" Grid.Row="2" Grid.Column="0" BackgroundColor="Transparent" Clicked="buttonClick" VerticalOptions="CenterAndExpand" HorizontalOptions="Fill" HeightRequest="{Binding Width,Source= {x:Reference 20}}" />
<Button x:Name="21" ClassId="21" PropertyChanged="PropertyChanged" Text="" Grid.Row="2" Grid.Column="1" BackgroundColor="Transparent" Clicked="buttonClick" VerticalOptions="CenterAndExpand" HorizontalOptions="Fill" HeightRequest="{Binding Width,Source= {x:Reference 21}}" />
<Button x:Name="22" ClassId="22" PropertyChanged="PropertyChanged" Text="" Grid.Row="2" Grid.Column="2" BackgroundColor="Transparent" Clicked="buttonClick" VerticalOptions="CenterAndExpand" HorizontalOptions="Fill" HeightRequest="{Binding Width,Source= {x:Reference 22}}" />
</Grid>
</StackLayout>
</ContentPage>
【问题讨论】:
-
我运行您发布的相同代码,它在我的最后工作正常,我只需将
RowSpacing和ColumnSpacing更改为更大的值,我还更改了按钮名称以开始字母。除了它显示 3x3 相同的正方形 -
@Brad Stephen 你解决了这个问题吗?
标签: xamarin button xamarin.forms grid square