Xamarin ListView(非窗体)绑定仅出现在一个单元格中

时间:2018-12-24 01:22:59

标签: c# listview xamarin xamarin.android

我在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>

1 个答案:

答案 0 :(得分:0)

没关系,我发现了问题。绑定值在DataTemplate之外声明,这显然导致listview仅显示列表中的最后一个值。我将声明移到DataTemplate内,现在可以正常使用了。