在Xamarin XAML c#win8中的Mill Game

时间:2016-12-27 16:10:26

标签: c# xaml xamarin windows-8

我想在Xamarin做一场Mill游戏。我想在网格中的特定位置按钮。这些位置来自我的ViewModel,但我不知道在浏览论坛后如何做到这一点。

我希望按钮之间有空格或行

<?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:view="clr-namespace:Malom.Portable.View"
             xmlns:controls="clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView"
             x:Class="Malom.Portable.View.GamePage">

   <ContentPage.Resources>
    <ResourceDictionary>
      <Style x:Key="EllipseStyle" TargetType="Button">
            <Style.Triggers>
                <DataTrigger TargetType="Button" Binding="{Binding Color}" Value="BluePlayer">
                    <Setter Property="BackgroundColor" Value="Blue" />
                </DataTrigger>
                <DataTrigger TargetType="Button" Binding="{Binding Color}" Value="RedPlayer">
                    <Setter Property="BackgroundColor" Value="Red" />
                </DataTrigger>
                <DataTrigger TargetType="Button" Binding="{Binding Color}" Value="Empty">
                    <Setter Property="BackgroundColor" Value="Black" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
  </ContentPage.Resources>
  <Grid>
    <ListView ItemsSource="{Binding Fields}" HasUnevenRows="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                            <Grid Padding="30,30,30,30" VerticalAlignment="Center">
                              <Button Grid.Row="{Binding Y}" Grid.Column="{Binding X}" Command="{Binding FieldChangeCommand}"></Button>
                            </Grid>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
  </Grid>
</ContentPage>

1 个答案:

答案 0 :(得分:0)

您可以遍历您的字段列表并以编程方式创建一个按钮并将其分配给您的网格:

foreach(var field in Fields)
{
  myGrid.Children.Add(new Button { Text = field.Name }, field.X, field.Y);
}