C# 如何让Xamarin ListView在不调整屏幕大小的情况下自动调整其行的大小?

C# 如何让Xamarin ListView在不调整屏幕大小的情况下自动调整其行的大小?,c#,xamarin.forms,windows-8.1,C#,Xamarin.forms,Windows 8.1,我在Xamarin应用程序中有一个列表视图,定义如下: listView = new ListView { HorizontalOptions = LayoutOptions.Fill, HasUnevenRows = true, RowHeight = -1, ItemTemplate = new DataTemplate(() => {

我在Xamarin应用程序中有一个列表视图,定义如下:

        listView = new ListView
        {
            HorizontalOptions = LayoutOptions.Fill,
            HasUnevenRows = true,
            RowHeight = -1,
            ItemTemplate = new DataTemplate(() =>
            {
                var nameLabel = new Label
                {
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalTextAlignment = TextAlignment.Start
                };
                nameLabel.SetBinding(Label.TextProperty, "DisplayName");

                var statusLabel = new Label 
                {
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalTextAlignment = TextAlignment.End
                };
                statusLabel.SetBinding(Label.TextProperty, "Status");

                var grid = new Grid
                {
                    HorizontalOptions = LayoutOptions.Fill,
                    RowDefinitions = {
                        new RowDefinition { Height = GridLength.Auto }
                    },
                    ColumnDefinitions = {
                        new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                        new ColumnDefinition { Width = GridLength.Auto }
                    }
                };

                grid.Children.Add(nameLabel, 0, 0);
                grid.Children.Add(statusLabel, 1, 0);

                return new ViewCell
                {
                    View = grid
                };
            })
        };
当我第一次设置其
ItemsSource
属性时,行被剪裁如下:

        listView = new ListView
        {
            HorizontalOptions = LayoutOptions.Fill,
            HasUnevenRows = true,
            RowHeight = -1,
            ItemTemplate = new DataTemplate(() =>
            {
                var nameLabel = new Label
                {
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalTextAlignment = TextAlignment.Start
                };
                nameLabel.SetBinding(Label.TextProperty, "DisplayName");

                var statusLabel = new Label 
                {
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalTextAlignment = TextAlignment.End
                };
                statusLabel.SetBinding(Label.TextProperty, "Status");

                var grid = new Grid
                {
                    HorizontalOptions = LayoutOptions.Fill,
                    RowDefinitions = {
                        new RowDefinition { Height = GridLength.Auto }
                    },
                    ColumnDefinitions = {
                        new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                        new ColumnDefinition { Width = GridLength.Auto }
                    }
                };

                grid.Children.Add(nameLabel, 0, 0);
                grid.Children.Add(statusLabel, 1, 0);

                return new ViewCell
                {
                    View = grid
                };
            })
        };

如果我调整屏幕大小,它们会被调整到正确的大小:

如果我再次更改
itemsource
,它们将返回被剪裁状态。调用
UpdateChildrenLayout
ForceLayout
无效


如何让ListView正确调整其行的大小,而不必要求用户调整其窗口的大小?这将是一个艰难的移动要求

如果ListView是全屏视图,则设置
VerticalOptions=FillAndExpand
。 不要静态设置
行高。因为您有
hasRows=true
它会根据其中内容的大小来调整不同行的大小。 还为listview的ItemTemplate内的控件提供
垂直选项
,以便正确调整其大小