Xamarin表单 - 使用C#向BoxView附加标签

时间:2017-10-23 21:22:09

标签: c# xaml xamarin mobile xamarin.forms

我想使用C#将标签附加到框视图。标签必须与每行中的某些列相对应。

标签应填充标题下方的列,如下图所示:

enter image description here

然而,在将内容附加到Grid元素之后,它变得混乱。以下是我的代码

的.xaml

    <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="{StaticResource dividerThickness}" />
      <RowDefinition Height="40" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="{StaticResource dividerThickness}" />
      <ColumnDefinition Width="45" />
      <ColumnDefinition Width="{StaticResource dividerThickness}" />
      <ColumnDefinition Width="*" />
      <ColumnDefinition Width="{StaticResource dividerThickness}" />
      <ColumnDefinition Width="*" />
      <ColumnDefinition Width="{StaticResource dividerThickness}" />
      <ColumnDefinition Width="45" />
      <ColumnDefinition Width="{StaticResource dividerThickness}" />
      <ColumnDefinition Width="45" />
      <ColumnDefinition Width="{StaticResource dividerThickness}" />
      <ColumnDefinition Width="45" />
      <ColumnDefinition Width="{StaticResource dividerThickness}" />
      <ColumnDefinition Width="45" />
      <ColumnDefinition Width="{StaticResource dividerThickness}" />
  </Grid.ColumnDefinitions>
  <!-- Vertical Lines -->
  <BoxView Grid.Row="0" Grid.Column="0" Grid.RowSpan="7" />
  <BoxView Grid.Row="0" Grid.Column="2" Grid.RowSpan="7" />
  <BoxView Grid.Row="0" Grid.Column="4" Grid.RowSpan="7" />
  <BoxView Grid.Row="0" Grid.Column="6" Grid.RowSpan="7" />
  <BoxView Grid.Row="0" Grid.Column="8" Grid.RowSpan="7" />
  <BoxView Grid.Row="0" Grid.Column="10" Grid.RowSpan="7" />
  <BoxView Grid.Row="0" Grid.Column="12" Grid.RowSpan="7" />
  <BoxView Grid.Row="0" Grid.Column="14" Grid.RowSpan="7" />

<Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="14" Text="Title" Style="{StaticResource headerRowStyle}"  />
<BoxView Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="14" />
<Label Grid.Row="2" Grid.Column="1" Text="Head 1" Style="{StaticResource resultTypeRow}" />
<Label Grid.Row="2" Grid.Column="3" Text="Head 2" Style="{StaticResource resultTypeRow}" />
<Label Grid.Row="2" Grid.Column="5" Text="Head 3" Style="{StaticResource resultTypeRow}" />
<Label Grid.Row="2" Grid.Column="7" Text="Head 4" Style="{StaticResource resultTypeRow}" />
<Label Grid.Row="2" Grid.Column="9" Text="Head 5" Style="{StaticResource resultTypeRow}" />
<Label Grid.Row="2" Grid.Column="11" Text="Head 6" Style="{StaticResource resultTypeRow}" />
<Label Grid.Row="2" Grid.Column="13" Text="Head 7" Style="{StaticResource resultTypeRow}" />
</Grid>

data.xaml.cs

protected override void OnAppearing()
{
     base.OnAppearing();
     var row = 3;
     foreach (var result in Results)
     {
           var dist = new Label
           {
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center,
                FontSize = 9,
                Text = result.Dist.ToString(),
            };
            var cycle = new Label
            {
                 HorizontalOptions = LayoutOptions.Center,
                 VerticalOptions = LayoutOptions.Center,
                 FontSize = 9,
                 Text = result.cycle.ToString(),
            };
            //REST OF LABELS HERE THEN APPEND TO PANEL 
            //Panel is the x:Name of the Grid Element

            Panel.Children.Add(dist,1,row);
            Panel.Children.Add(cycle, 3, row);
            Panel.Children.Add(data, 5, row);
            Panel.Children.Add(data2, 7, row);
            Panel.Children.Add(data3, 9, row);
            Panel.Children.Add(data4, 11, row);
            Panel.Children.Add(data5, 13, row);
            row++;
            count++;
     }
 }
  

知道我需要做些什么才能将我的结果巧妙地整合到这些列中?

0 个答案:

没有答案