Xamarin仅在第一行显示自定义单元格

问题描述 投票:0回答:3

我是Xamarin表格的新手,我想创建对驾驶员有用的应用程序。我正在尝试创建具有多个stacklayouts的自定义单元格。但是在列表视图中,仅显示我的自定义单元格的第一行。这是我的自定义单元格代码:

<ListView x:Name="ListViewRides">
<ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <StackLayout Orientation="Vertical" Margin="15">
        <StackLayout Orientation="Horizontal">
          <Label x:Name="LabelLocationFrom" Text="{Binding PlaceFrom}" Margin="0, 0, 5,0"/>
          <Label Text="->"/>
          <Label x:Name="LabelLocationTo" Text="{Binding PlaceTo}" Margin="5, 0, 0, 0"/>
        </StackLayout>
        <StackLayout Orientation="Horizontal">
          <Label x:Name="LabelTimeFrom" Text="{Binding TimeFrom}" Margin="0, 0, 5,0"/>
          <Label Text="->"/>
          <Label x:Name="LabelTimeTo" Text="{Binding TimeTo}" Margin="5, 0, 0, 0"/>
        </StackLayout>
        <Label x:Name="LabelCustomer" Text="{Binding Customer}"></Label>
      </StackLayout>
    </ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>

这里是模拟器中显示的结果Result in simulator

我不知道我在哪里犯错,有人可以帮我吗?为了完整概述,我还添加了ViewModel代码:

public class RidesViewModel:Bindable
{
    public string PlaceFrom { get; set; }
    public string PlaceTo { get; set; }
    public DateTime TimeFrom { get; set; }
    public DateTime TimeTo { get; set; }
    public string Load { get; set; }
    public int Weight { get; set; }
    public string Note { get; set; }
    public string ContactPersonFrom { get; set; }
    public string ContactPersonTo { get; set; }
    public string Customer { get; set; }
    public string Trailer { get; set; }
    public bool IsReadyToShow { get; set; }
    public string RideState { get; set; }
}

谢谢您的建议。我想这是显而易见的,我现在看不到。

listview xamarin xamarin.forms custom-cell
3个回答
2
投票

您看不到它可能是由于它不在视线内,但它在那里。

RowHeightListView属性设置为较高的值以进行验证。

如果您没记错的话,RowHeight应该能够计算出设置为-1并将HasUnevenRows设置为true的时间。但是,根据我的经验,这并不总是可行。要使其正常工作,您可能必须使用一些自定义渲染器。


1
投票

您必须将ListView(...或任何视图)设置为HasUnevenRows = True,并将VerticalOptions设置为“ CenterAndExpand”或“ FillAndExpand”或其他合适的东西。


0
投票

经过大量研究,我创建了带有测试数据的打击代码Listview。

只需复制并粘贴代码并检查结果。

    <ListView HasUnevenRows="true"
              RowHeight="200">
        <ListView.ItemsSource>
            <x:Array Type="{x:Type x:String}">
                <x:String>Item One</x:String>
                <x:String>Item Two</x:String>
                <x:String>Item Three</x:String>
                <x:String>Item Four</x:String>
                <x:String>Item Five</x:String>
            </x:Array>
        </ListView.ItemsSource>
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Frame BorderColor="Red" Padding="0">
                        <Grid Margin="10"
                              BackgroundColor="Brown">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="90" />
                                <RowDefinition Height="90"/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>

                            <BoxView Grid.Row="0"
                                     Grid.Column="0"
                                     Color="Yellow" />
                            <BoxView Grid.Row="0"
                                     Grid.Column="1"
                                     Color="Black" />
                            <BoxView Grid.Row="1"
                                     Grid.Column="0"
                                     Color="Green" />
                            <BoxView Grid.Row="1"
                                     Grid.Column="1"
                                     Color="Blue" />
                            <Label Text="{Binding}"
                                   FontAttributes="Bold"
                                   TextColor="Red"
                                   HorizontalOptions="CenterAndExpand"
                                   VerticalOptions="CenterAndExpand"/>
                        </Grid>
                    </Frame>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
© www.soinside.com 2019 - 2024. All rights reserved.