【问题标题】:ListView doesn't display data in xamarinListView 在 xamarin 中不显示数据
【发布时间】:2018-09-12 15:15:25
【问题描述】:

我是 xamarin 的新手,正在尝试显示列表视图。我的尝试如下。

<ContentPage.Content>
    <StackLayout>
        <ListView x:Name="ComListView" RowHeight="80">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <Grid Margin="8">
                                <Label Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="2" Text="hello world" FontAttributes="Bold"  />
                            </Grid>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

但是当我运行应用程序时,即使我导航到正确的页面;什么都没有显示。对于任何其他视图,它将显示正确的输出。那么我在 listview 上哪里会出错呢?

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    您必须将项目放入ListView。目前,您只指定ItemTemplate,它决定了项目的外观(在您的情况下,它们看起来都一样(即带有“hello world”文本的标签))。但是ListView 是空的,所以没有要显示的项目。

    您可以使用ListViewItemsSource 属性设置项目。

    通常,您在ItemTemplate 中使用Binding 来显示项目内容。

    假设我们有一个班级Person

    class Person
    {
        public string Name { get; set; }
    }
    

    然后创建Person 对象的List 并将其添加到代码中的ListView

    var personnel = new List<Person>
    {
        new Person { Name = "Booker" },
        new Person { Name = "Elizabeth" }
    };
    
    ComListView.ItemsSource = personnel;
    

    并且在您的 XAML 中,您必须设置 ItemTemplate 以便它显示每个 PersonName

    <Label Text="{Binding Name, Mode=OneWay}" />
    

    注意:如果您有 ViewModel,也可以从 XAML 绑定到 ItemsSource

    【讨论】:

      【解决方案2】:

      您没有将数据绑定到列表视图。 使用 ItemSource 属性绑定来自 xaml、隐藏代码或使用绑定从 ViewModel 填充的数据

      【讨论】:

        猜你喜欢
        • 2018-09-14
        • 1970-01-01
        • 1970-01-01
        • 2021-05-22
        • 2021-10-16
        • 1970-01-01
        • 1970-01-01
        • 2018-01-21
        • 1970-01-01
        相关资源
        最近更新 更多