C# 如何将ListView的高度设置为所有视图的高度';是否将ListViewItems以Xamarin.Form的形式放在一起?

C# 如何将ListView的高度设置为所有视图的高度';是否将ListViewItems以Xamarin.Form的形式放在一起?,c#,listview,xamarin.forms,C#,Listview,Xamarin.forms,看起来我只需要将ListView的高度设置为项的数量乘以项的高度 但是如何获取ListViewItem的高度呢 myListView.RowHeight仅显示-1 我这样声明ListView: DataTemplate dTemplate = new DataTemplate(() => { StackLayout sl = new StackLayout { VerticalOptions = LayoutOptions.FillAndExpand,

看起来我只需要将
ListView
的高度设置为
项的数量
乘以
项的高度

但是如何获取
ListViewItem
的高度呢

myListView.RowHeight
仅显示
-1

我这样声明
ListView

DataTemplate dTemplate = new DataTemplate(() => {
    StackLayout sl = new StackLayout {
        VerticalOptions = LayoutOptions.FillAndExpand,
        Orientation = StackOrientation.Horizontal,
        Padding = new Thickness(20, 10, 0, 10),
        Spacing = 20
    };

    var temp = new Label { FontSize = 20 };
    temp.SetBinding(Label.TextProperty, ".");

    sl.Children.Add(temp);

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

ListView myListView = new ListView {
    VerticalOptions = LayoutOptions.FillAndExpand,
    ItemsSource = stringList,
    ItemTemplate = dTemplate
    BackgroundColor = Color.Red
};
我使用以下结构(我不使用XAML):


...
这就是我的页面在0
ListViewItems
的情况下的外观(ListView的大小不随值的数量而变化):


您的
列表视图垂直选项
是否应该设置为
LayoutOptions.FillAndExpand
,以便它可以扩展以适应所有子项

更新

您的
布局选项
应仅设置为
填充
,无需展开。它将适合你的屏幕高度或布局,如果你定义一个

代码:

List stringList=新列表{“a”、“b”、“c”、“c”、“a”、“b”、“c”、“a”、“b”、“c”、“c”、“a”、“b”、“c”、“a”、“b”、“b”、“c”、“a”、“b”、“b”、“b”、“b”、“b”、“a”、“b”、“a”、“b”、“a”、“b”、“b”、“c”、“a”、“b”、“a”、“b”、“c”、“a”、“b”、“c”、“a”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“c”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“b”、“c”,“a”,“b”,“c”,};
DataTemplate dTemplate=新的DataTemplate(()=>
{
StackLayout sl=新的StackLayout
{
方向=堆叠方向。水平,
填充=新厚度(20,10,0,10),
间距=20
};
var temp=新标签{FontSize=20};
临时设置绑定(Label.TextProperty,“.”;
sl.Children.Add(临时);
返回新的ViewCell{View=sl};
});
ListView myListView=新建ListView
{
垂直选项=布局选项。填充,
ItemsSource=stringList,
ItemTemplate=dTemplate,
背景颜色=颜色。红色
};
this.Content=myListView;

这串代码对我的项目来说还行,但我非常怀疑这是一个好的实践

myListView.RequestHeight = number_of_items * 45;

哪个元素是您的
myListView
的父元素?我根据您的代码更改了所有内容,但是
ListView
的大小仍然是固定的。唯一不同的是,我的
StackLayout
有许多控件,如
子控件
。难道你的
列表视图
没有与设备相同的高度吗?很遗憾,但没有。添加了屏幕截图。StackLayout就足够了;如果项目总高度大于屏幕高度,则listview将过大。
List<string> stringList = new List<string> { "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", };
        DataTemplate dTemplate = new DataTemplate(() =>
        {
            StackLayout sl = new StackLayout
            {
                Orientation = StackOrientation.Horizontal,
                Padding = new Thickness(20, 10, 0, 10),
                Spacing = 20
            };

            var temp = new Label { FontSize = 20 };
            temp.SetBinding(Label.TextProperty, ".");

            sl.Children.Add(temp);

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

        ListView myListView = new ListView
        {
            VerticalOptions = LayoutOptions.Fill,
            ItemsSource = stringList,
            ItemTemplate = dTemplate,
            BackgroundColor = Color.Red
        };

        this.Content = myListView;
myListView.RequestHeight = number_of_items * 45;