以 xamarin 形式删除列表视图的空白空间

Man*_*ram 4 xamarin.forms.listview

我正在为我的项目使用 xamarin.forms 列表视图。设计如下:

<ContentView x:Name="Overlay" IsVisible="False"
                         VerticalOptions="Center" HorizontalOptions="Center" Margin="10,20,10,30"
                         AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" BackgroundColor="LightGray">
                <StackLayout Spacing="0">
                    <StackLayout Padding="20" BackgroundColor="#9C6114" Spacing="0">
                        <Label Text="Country Name" TextColor="White" FontAttributes="Bold" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"></Label>
                    </StackLayout>
                    <StackLayout Spacing="0">
                <ListView x:Name="CountryList" HasUnevenRows="True" Margin="10,0,0,0">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Orientation="Horizontal">
                                    <Switch IsToggled="{Binding IsToggle}"></Switch>
                                    <Label Text="{Binding CountryName}"></Label>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                        <ListView.Footer>
                            <StackLayout Orientation="Horizontal">
                                <Button x:Name="btncancel" Text="Cancel" TextColor="White" BackgroundColor="#9C6114" HorizontalOptions="CenterAndExpand" Clicked="btncancel_Clicked"></Button>
                                <Button x:Name="btnsubmit" Text="Submit" TextColor="White" BackgroundColor="#9C6114" HorizontalOptions="CenterAndExpand" Clicked="btnsubmit_Clicked"></Button>
                            </StackLayout>            
                        </ListView.Footer>
                </ListView>
                    </StackLayout>
                </StackLayout>
            </ContentView>
Run Code Online (Sandbox Code Playgroud)

对于此列表视图,列表视图下方有空余空间。我无法删除列表视图中的多余空格。请参阅所附图片。在此处输入图片说明

请帮我解决这个问题。

jab*_*idi 6

我遇到了与此类似的问题。我点击了这个链接:https : //xamarinsharp.com/2017/05/20/xamarin-forms-listview-height-change-dynamically-using-mvvm-and-also-solve-empty-space-issue/

基本上,如果您还没有,您需要使用 MVVM 设置您的项目。在您的 XAML 中,您必须为列表视图提供一个 HeighRequest 属性并将其值设置为:HeighRequest="{Binding Height}"。在您的视图模型中,添加以下内容:

int_height;

公共整数高度

    {
        get { return _height; }
        set
        {
            _height = value;
            OnPropertyChanged("Height");
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后,在您的 viewModel 的构造函数(或您设置列表视图对象的任何地方)中,添加以下内容:

        Height = (**object**.Count * 60) + (**object**.Count * 10);
Run Code Online (Sandbox Code Playgroud)

这对我来说很好用!