我在我的Xamarin应用程序中有一个ListView,定义如下:
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
属性时,行被剪切了,如下图所示:
如果我调整屏幕大小,它们会被调整为正确的大小:
如果我再次更改ItemsSource
,它们又会被剪切。调用UpdateChildrenLayout
或ForceLayout
没有效果。我该如何让ListView正确调整行的大小,而不必要求用户调整其窗口大小? 这在移动设备上可能很困难!