簡體   English   中英

如何在自定義 Xamarin.Forms ViewCell 上的行之間添加分隔符空間?

[英]How to add a separator space between rows on custom Xamarin.Forms ViewCell?

Xamarin 論壇上的這個問題中,Craig Dunn 教授了如何使用框架創建單元格。

我想在每個單元格之間添加一個空格。

目前這些單元格似乎是粘在一起的,而ViewCell沒有空間屬性。

如何在自定義 Xamarin.Forms ViewCell 上的行之間添加分隔符空間?

您只需要進一步自定義MenuCell的布局即可實現此目的。

下面顯示的是一個版本,它使用另一個Xamarin.Forms.Frame在每個項目之間創建一個間距,並進行一些其他修改: -

XAML頁面: -

<ListView x:Name="lstItems" />

XAML代碼 - 背后: -

lstItems.ItemTemplate = new DataTemplate(typeof(Classes.MenuCell));
lstItems.ItemsSource = new string[] { "Apples", "Bananas", "Pears", "Oranges" };

ViewCell類: -

public class MenuCell : ViewCell
{
    public MenuCell()
    {
        Label objLabel = new Label
        {
            YAlign = TextAlignment.Center,
            TextColor = Color.Yellow,                
        };
        objLabel.SetBinding(Label.TextProperty, new Binding("."));


        StackLayout objLayout = new StackLayout
        {
            Padding = new Thickness(20, 0, 0, 0),
            Orientation = StackOrientation.Horizontal,
            HorizontalOptions = LayoutOptions.StartAndExpand,
            Children = { objLabel }
        };

        Frame objFrame_Inner = new Frame
        {
            Padding = new Thickness(15, 15, 15, 15),
            HeightRequest = 36,
            OutlineColor = Color.Accent,
            BackgroundColor = Color.Blue,
            Content = objLayout,                
        };

        Frame objFrame_Outer = new Frame
        {
            Padding = new Thickness(0, 0, 0, 10),
            Content = objFrame_Inner
        };

        View = objFrame_Outer;            
    }
}

將導致以下結果: -

ListView與每個項目之間的間距。

我的xaml示例:

 <ListView BackgroundColor="Gray" SeparatorVisibility="None" ItemsSource="{Binding Shipments}" x:Name="lstShipments" RowHeight="60">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <StackLayout Padding="0,0,0,1">
            <Grid VerticalOptions="Fill" BackgroundColor="White">
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"></ColumnDefinition>
                <ColumnDefinition Width="60"></ColumnDefinition>
              </Grid.ColumnDefinitions>
              <Label Grid.Column="0" Grid.Row="0" Text="{Binding DestinationCountry}" FontSize="16" />
              <Image Grid.Column="1" Grid.Row="0" Source="box32.png" />
              <Label Grid.Column="0" Grid.Row="1" Text="{Binding ExchangeOfficeDestinationTitle}" FontSize="16" />
              <Label Grid.Column="1" Grid.Row="1" Text="{Binding ShipmentNum}" FontSize="10" />

            </Grid>
          </StackLayout>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

在此輸入圖像描述

博客保持它們分離部分中描述了一種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM