如何使用XAML在ListView中实现ItemTemplate

Sco*_*rod 2 xamarin.forms

如何使用XAML在ListView中实现ItemTemplate?

如果我不使用ItemTemplate,那么我的绑定工作,我没有收到任何错误.但是,我想格式化我的列表视图.因此,我试图使用ItemTemplate但继续遇到运行时异常:

Xamarin.Forms.Xaml.XamlParseException:位置30:30.无法分配属性"View":"Xamarin.Forms.TextCell"和"X ..."之间的类型不匹配

我使用以下XAML收到运行时异常:

<ListView Grid.Row="1" Grid.Column="0" ItemsSource="{Binding Services}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <TextCell Text="{Binding Name}" Detail="{Binding Description}" />
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

有什么建议?

Dan*_*rda 7

  • ViewCell是一个可用于创建自定义单元格布局的类
  • TextCell是具有预定义布局的预定义单元格.

您不能在另一个Cell中使用Cell(例如,ViewCell中的TextCell).

您可以像这样创建自定义单元格:

<ListView Grid.Row="1" Grid.Column="0" ItemsSource="{Binding Services}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout Orientation="Vertical">
                        <Label Text="{Binding Name}" />
                        <Label Text="{Binding Description}" />
                    </StackLayout>                
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

或使用预定义的:

<ListView Grid.Row="1" Grid.Column="0" ItemsSource="{Binding Services}">
    <ListView.ItemTemplate>
        <DataTemplate>
          <TextCell Text="{Binding Name}" Detail="{Binding Description}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

预定义单元格列表如下:http://developer.xamarin.com/guides/cross-platform/xamarin-forms/controls/cells/