删除Xamarin形式的ListView的空白空间

时间:2018-10-12 07:50:25

标签: xamarin.forms.listview

我在为我的项目使用xamarin.forms listview。设计如下:

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

对于此列表视图,列表视图下方有空白区域。我无法删除列表视图中的多余空格。请查看所附图片。enter image description here

请帮助我解决此问题。

2 个答案:

答案 0 :(得分:0)

我有一个与此类似的问题。我点击了以下链接: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;

public int Height

    {
        get { return _height; }
        set
        {
            _height = value;
            OnPropertyChanged("Height");
        }
    }

然后,在viewModel的构造函数中(或设置listview对象的任何位置),添加以下内容:

        Height = (**object**.Count * 60) + (**object**.Count * 10);

这对我来说很好!

答案 1 :(得分:0)

经过多次搜索,我发现以下内容对我有用:我有一个列表视图模板,其中包含一个包含网格的框架。在我的 XAML 文件中,我为网格添加了 SizeChanged 事件和 BindingContext:

...
<ListView x:Name="worksiteList"
    ItemsSource="{Binding WorksiteList}"
    SelectionMode="Single"
    SeparatorVisibility="None"
    HasUnevenRows="True">
<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <StackLayout Orientation="Vertical" Margin="0" Padding="0">
                <Frame ...>
                    <Grid
                        SizeChanged="worksiteGrid_SizeChanged"
                        BindingContext="{Binding .}"
                        RowDefinitions="Auto,Auto"
                        ColumnDefinitions="50*,40*">

然后在后面的代码中,我添加了 SizeChanged 事件处理程序和 OnAppearing 的覆盖,如下所示:

private Dictionary<int, double> _worksiteFrameSizeMap = new Dictionary<int, double>();

public void worksiteGrid_SizeChanged(object sender, EventArgs e)
{
    var grid = sender as Grid;

    if (grid != null)
    {
        var frame = grid.Parent as Frame;

        if (frame != null)
        {
            var worksite = grid.BindingContext as Worksite;

            if (worksite != null)
            {
                var frameHeight = 0.0;

                frameHeight += frame.Height;
                frameHeight += frame.Margin.Top;
                frameHeight += frame.Margin.Bottom;
                _worksiteFrameSizeMap[worksite.WorksiteId] = frameHeight;
            }
        }
    }
}

protected override void OnAppearing()
{
    base.OnAppearing();
    var worksiteListViewHeight = 0.0;

    foreach (var item in _worksiteFrameSizeMap)
    {
        worksiteListViewHeight += item.Value;
    }
    worksiteList.HeightRequest = worksiteListViewHeight;
}

这是我获得列表高度的唯一方法,因此最后一个列表项下方没有空格(我尝试了许多其他无效的方法)。不幸的是,页面出现时会有轻微的闪烁。我倾向于认为 ListView 控件有一个错误,因为 ListView 应该自动执行此操作(是否有人想要列表下方的空白空间?)并且没有简单的方法来访问列表中的项目大小。