首页 > 解决方案 > Xamarin ListView(不是 Forms)绑定只出现在一个单元格中

问题描述

我在 Android Xamarin 页面中有一个用 C# 实现的 ListView。每个 ListView 项都有标签和绑定数据。渲染时,标签在所有项目中显示良好,但绑定数据仅显示在最后一项中(其他项目为空白)。我已验证数据在用作数据源的列表中。

我在 XAML 中实现了几乎相同的页面,并且工作正常。我不明白为什么 C# 版本中只填充了一项。

我确实注意到我在运行时在 C# 实现上收到了一些“requestLayout() 不正确地调用...”错误,但一直无法弄清楚为什么会这样。

    public EditRoutines()
    {
        Title = "Edit Routines";

        Label label0 = new Label { Text = "Count: ", TextColor = Color.Gray, FontAttributes = FontAttributes.None, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)) };

        lName = new Label { TextColor = Color.Blue, FontAttributes = FontAttributes.None, FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)) };
        lName.SetBinding(Label.TextProperty, "name");

        lCount = new Label {  TextColor = Color.Gray, FontAttributes = FontAttributes.None, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)) };
        lCount.SetBinding(Label.TextProperty, "count");

        lDuration = new Label { TextColor = Color.Gray, FontAttributes = FontAttributes.None, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)) };
        lDuration.SetBinding(Label.TextProperty, "duration");

        lInterval = new Label { TextColor = Color.Gray, FontAttributes = FontAttributes.None, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)) };
        lInterval.SetBinding(Label.TextProperty, "interval");

        DataTemplate dt = new DataTemplate(() =>
        {
            StackLayout sl = new StackLayout
            {
                HorizontalOptions = LayoutOptions.Fill,
                Orientation = StackOrientation.Horizontal,
                Children = {
                    new StackLayout
                    {
                        VerticalOptions = LayoutOptions.StartAndExpand,
                        Orientation = StackOrientation.Vertical,
                        Children = {
                            lName,
                            new StackLayout
                            {
                                Orientation = StackOrientation.Horizontal,
                                VerticalOptions = LayoutOptions.StartAndExpand,
                                Children = {
                                    new Label { Text="Count: ", TextColor=Color.Gray, FontAttributes= FontAttributes.None, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)) },                        
                                    lCount
                                }
                            },
                            new StackLayout
                            {
                                Orientation = StackOrientation.Horizontal,
                                VerticalOptions = LayoutOptions.StartAndExpand,
                                Children = {
                                    new Label { Text = "Duration: ", TextColor=Color.Gray, FontAttributes= FontAttributes.None, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label))},
                                    lDuration
                                }
                            },
                            new StackLayout
                            {
                                Orientation = StackOrientation.Horizontal,
                                VerticalOptions = LayoutOptions.StartAndExpand,
                                Children = {
                                    new Label { Text = "Interval: ", TextColor=Color.Gray, FontAttributes= FontAttributes.None, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)) },
                                    lInterval
                                }
                            }
                        }
                    }
                }
            };


            return new ViewCell() { View = sl };
        });

        Content = new StackLayout
        {
            Margin = new Thickness(20),
            Children = {
            new ListView { ItemsSource = Global.routines.routineList, ItemTemplate = dt, Margin = new Thickness(0, 20, 0, 0), HasUnevenRows = true }
            }
        };

    }

这是有效的 XAML。

<ListView x:Name="listviewRoutines" HorizontalOptions="FillAndExpand" HasUnevenRows="True" ItemSelected="listviewRoutines_ItemSelected">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout HorizontalOptions="Fill" Orientation="Horizontal">
                    <StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical" >
                        <Label Text="{Binding name}" HorizontalOptions="StartAndExpand" TextColor="Blue"  FontAttributes="Bold" FontSize="Default"/>
                        <!--
                        <Label Text="{Binding description}" HorizontalOptions="StartAndExpand" TextColor="Gray"  FontAttributes="None" FontSize="Small"/>
                        -->
                        <StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Horizontal">
                            <Label Text="Count: "  TextColor="Gray"  FontAttributes="None" FontSize="Small"/>
                            <Label Text="{Binding count}" TextColor="Gray"  FontAttributes="None" FontSize="Small"/>
                        </StackLayout>
                        <StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Horizontal">
                            <Label Text="Duration: "  TextColor="Gray"  FontAttributes="None" FontSize="Small"/>
                            <Label Text="{Binding duration}" TextColor="Gray"  FontAttributes="None" FontSize="Small"/>
                        </StackLayout>
                        <StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Horizontal">
                            <Label Text="Interval: "  TextColor="Gray"  FontAttributes="None" FontSize="Small"/>
                            <Label Text="{Binding interval}" TextColor="Gray"  FontAttributes="None" FontSize="Small"/>
                        </StackLayout>
                        <StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Horizontal">
                            <Label Text="When: "  TextColor="Gray"  FontAttributes="None" FontSize="Small"/>
                            <Label Text="{Binding when}" TextColor="Gray"  FontAttributes="None" FontSize="Small"/>
                        </StackLayout>
                    </StackLayout>
                </StackLayout>
            </ViewCell>

        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

标签: c#listviewxamarinxamarin.android

解决方案


没关系,我发现了问题。绑定值在 DataTemplate 之外声明,这显然会导致列表视图仅显示列表中的最后一个值。我在 DataTemplate 中移动了声明,它现在可以正常工作了。


推荐阅读