2016-12-27 44 views
0

我想在Xamarin製造磨機遊戲。我想要網格中特定位置的按鈕。這些位置來自我的ViewModel這樣,但我不知道如何做到這一點後瀏覽論壇。Xamarin XAML磨機遊戲c#win8

我想按鈕

<?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>
之間的空白空間或線

+0

如果你想在網格中的特定位置使用按鈕,請不要將它們放在ListView中。以編程方式將它們直接放入網格中。 – Jason

+0

我如何給我的網格項目源?或者我應該使用另一個物品容器?我希望我的按鈕在遊戲 – szarkabarna

+0

等特定位置不能綁定到網格 - 這就是爲什麼我說你必須以編程方式分配按鈕 – Jason

回答

0

你可以通過你的字段列表循環和編程方式創建一個按鈕,並將其分配給您的網格:

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

即時通訊只允許使用XAML我不能使用任何後臺代碼的觀點...但謝謝。我必須在MVVM體系結構中執行此操作。而且我不能在ViewModel中使用任何代碼來創建視圖,如添加新按鈕 – szarkabarna

+1

@szarkabarna,那麼您還沒有理解MVVM。 – Cheesebaron

+0

我必須這樣做... – szarkabarna