Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListView中的空间比我需要的多_C#_.net_Listview_Xamarin_Xamarin.forms - Fatal编程技术网

C# ListView中的空间比我需要的多

C# ListView中的空间比我需要的多,c#,.net,listview,xamarin,xamarin.forms,C#,.net,Listview,Xamarin,Xamarin.forms,我使用StackLayout和ListView来显示视图的某些部分,但是ListView占用的空间比我需要的多,并且在列表的最后一行和概要文件延续之间留有空白。我的ListView的行数似乎超过了实际列表的长度,或者它有一个固定的高度,我不知道。。。这是我的XAML: <ScrollView> <StackLayout Orientation="Vertical" Spacing="10"> <Label Text="{Binding Establis

我使用StackLayout和ListView来显示视图的某些部分,但是ListView占用的空间比我需要的多,并且在列表的最后一行和概要文件延续之间留有空白。我的ListView的行数似乎超过了实际列表的长度,或者它有一个固定的高度,我不知道。。。这是我的XAML:

<ScrollView>
  <StackLayout Orientation="Vertical" Spacing="10">

    <Label Text="{Binding Establishment.Name}" TextColor="Black" HorizontalOptions="StartAndExpand" Style="{DynamicResource TitleStyle}" FontAttributes="Bold"/>

    <Label Text="{Binding Establishment.Category.Name,  StringFormat='Categoria: {0}'}" TextColor="Black" HorizontalOptions="StartAndExpand"  FontAttributes="Bold"/>

    <Label Text="{Binding Establishment.Description}" TextColor="Black" HorizontalOptions="StartAndExpand" LineBreakMode="WordWrap" XAlign="Start"/>

    <Label Text="Contatos" TextColor="Black" HorizontalOptions="StartAndExpand"  FontAttributes="Bold"/>

    <ListView  ItemsSource="{Binding Establishment.Contacts}" VerticalOptions="Start" HasUnevenRows="True">
      <ListView.ItemTemplate>
        <DataTemplate>
          <TextCell Text="{Binding Value}" Detail="{Binding StringType}" DetailColor="Gray"/>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

    <Label Text="Endereço" TextColor="Black" HorizontalOptions="StartAndExpand"  FontAttributes="Bold"/>

    <Label Text="{Binding Establishment.FullAddress}" TextColor="Black" HorizontalOptions="StartAndExpand"  />

    <Button Text="Ver no mapa"/>

  </StackLayout>
</ScrollView>


我怎样才能修好它?我看到有人在使用
hasrows=“True”
,但这对我不起作用。

首先,Xamarin建议不要在
滚动视图中放置
列表视图,因为它们都提供滚动功能,并且相互争斗。我曾亲自尝试过这个方法,它经常会导致奇怪的行为,尤其是在Android上

相反,我要做的是将
ListView.Header
ListView.HeaderTemplate
一起使用,将
ListView.Footer
ListView.Footer模板一起使用。Xamarin链接这允许您的确切场景,但没有奇怪的行为问题。如果你想看一个例子,让我知道

如果您必须使用当前布局,您可以尝试查看有关如何使用特定的
高度
宽度
布局选项来调整
列表视图大小的帖子,因为它不应该适合其内容的大小。在那篇文章中有几个解决方案,我还没有尝试过

在post中,给出的一个解决方法是将
列表视图添加到
堆栈布局中,然后将其添加到另一个
堆栈布局中。然后将内部
StackLayout
VerticalOptions
设置为
LayoutOptions.FillAndExpand

所以它可能看起来像这样:

<ScrollView>
  <StackLayout Orientation="Vertical" Spacing="10">

    ....

    <StackLayout VerticalOptions="FillAndExpand">
      <ListView ItemsSource="{Binding Establishment.Contacts}" VerticalOptions="Start" HasUnevenRows="True">
        <ListView.ItemTemplate>
          <DataTemplate>
            <TextCell Text="{Binding Value}" Detail="{Binding StringType}" DetailColor="Gray"/>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </StackLayout>

    ....

  </StackLayout>
</ScrollView>
    <StackLayout x:Name="ContactsList" Orientation="Vertical" Spacing="10" Padding="8,0">
      <!-- Empty because it will be filled in code mode -->
    </StackLayout>

....
....

正如hvaughan3所说,这确实不是一个好的做法。但我也有同样的情况,我对行为采取了变通办法:

public class ListViewHeightBehavior : Behavior<ListView>
{
    private ListView _listView;

    public static readonly BindableProperty ExtraSpaceProperty =
        BindableProperty.Create(nameof(ExtraSpace),
                                typeof(double),
                                typeof(ListViewHeightBehavior),
                                0d);

    public double ExtraSpace
    {
        get { return (double)GetValue(ExtraSpaceProperty); }
        set { SetValue(ExtraSpaceProperty, value); }
    }

    protected override void OnAttachedTo(ListView bindable)
    {
        base.OnAttachedTo(bindable);

        _listView = bindable;
        _listView.PropertyChanged += (s, args) =>
        {
            var count = _listView.ItemsSource?.Count();
            if (args.PropertyName == nameof(_listView.ItemsSource)
                    && count.HasValue
                    && count.Value > 0)
            {
                _listView.HeightRequest = _listView.RowHeight * count.Value + ExtraSpace;
            }
        };
    }
}
公共类ListViewHeightBehavior:Behavior
{
私有ListView _ListView;
公共静态只读BindableProperty ExtraSpaceProperty=
BindableProperty.Create(nameof(ExtraSpace),
类型(双),
类型(ListViewHeightBehavior),
0d);
公共双外部空间
{
获取{return(double)GetValue(ExtraSpaceProperty);}
set{SetValue(ExtraSpaceProperty,value);}
}
受保护的覆盖无效数据到(ListView可绑定)
{
碱。可粘合的DTO(可粘合);
_listView=可绑定;
_listView.PropertyChanged+=(s,args)=>
{
变量计数=_listView.ItemsSource?.count();
if(args.PropertyName==nameof(_listView.ItemsSource)
&&count.HasValue
&&计数(数值>0)
{
_listView.HeightRequest=\u listView.RowHeight*count.Value+外部空间;
}
};
}
}
XAML:


//你的模板

谢谢你们,但对我来说什么都不管用。我自己认为的解决方案肯定不是最好的,但它对我很有效

在XAML中,我留下了一个空的StackLayout,如下所示:

<ScrollView>
  <StackLayout Orientation="Vertical" Spacing="10">

    ....

    <StackLayout VerticalOptions="FillAndExpand">
      <ListView ItemsSource="{Binding Establishment.Contacts}" VerticalOptions="Start" HasUnevenRows="True">
        <ListView.ItemTemplate>
          <DataTemplate>
            <TextCell Text="{Binding Value}" Detail="{Binding StringType}" DetailColor="Gray"/>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </StackLayout>

    ....

  </StackLayout>
</ScrollView>
    <StackLayout x:Name="ContactsList" Orientation="Vertical" Spacing="10" Padding="8,0">
      <!-- Empty because it will be filled in code mode -->
    </StackLayout>
我这样做是因为列表不是一个真正的列表视图,因为我不需要直接在列表中滚动函数,它只是视图的另一部分。我认为它不会导致任何性能问题,因为它将有少量的项目(最多5个)。我认为这是帮助他人解决这个问题的正确答案


请告诉我,如果这是一个对其他人不起作用的解决方案,我会找到另一种解决方法。

我对Yehor Hromadskyi的解决方案有意见

  • 首先,IEnumerable没有计数方法。必须首先将其转换为IEnumerable
  • 其次,如果不设置行高,则整个列表将崩溃,出现相反的问题
下面是对代码的更改,以解决这些问题

public class ListViewHeightBehavior : Behavior<ListView>
{
    private ListView _listView;

    public static readonly BindableProperty ExtraSpaceProperty =
        BindableProperty.Create(nameof(ExtraSpace),
                                typeof(double),
                                typeof(ListViewHeightBehavior),
                                0d);

    public static readonly BindableProperty DefaultRowHeightProperty =
        BindableProperty.Create(nameof(DefaultRowHeight),
                          typeof(int),
                          typeof(ListViewHeightBehavior),
                          40);

    public int DefaultRowHeight
    {
        get => (int)GetValue(DefaultRowHeightProperty);
        set => SetValue(DefaultRowHeightProperty, value);
    }
    public double ExtraSpace
    {
        get { return (double)GetValue(ExtraSpaceProperty); }
        set { SetValue(ExtraSpaceProperty, value); }
    }

    protected override void OnAttachedTo(ListView bindable)
    {
        base.OnAttachedTo(bindable);

        _listView = bindable;
        _listView.PropertyChanged += (s, args) =>
        {
            var count = _listView.ItemsSource?.Cast<object>()?.Count();
            if (args.PropertyName == nameof(_listView.ItemsSource)
                    && count.HasValue
                    && count.Value > 0)
            {
                int rowHeight = _listView.RowHeight > 0 ? _listView.RowHeight : DefaultRowHeight;
                _listView.HeightRequest = rowHeight * count.Value + ExtraSpace;
            }
        };
    }
}
公共类ListViewHeightBehavior:Behavior
{
私有ListView _ListView;
公共静态只读BindableProperty ExtraSpaceProperty=
BindableProperty.Create(nameof(ExtraSpace),
类型(双),
类型(ListViewHeightBehavior),
0d);
公共静态只读BindableProperty DefaultRowHeightProperty=
BindableProperty.Create(nameof(DefaultRowHeight),
类型(int),
类型(ListViewHeightBehavior),
40);
公共int默认行高
{
get=>(int)GetValue(DefaultRowHeightProperty);
set=>SetValue(DefaultRowHeightProperty,value);
}
公共双外部空间
{
获取{return(double)GetValue(ExtraSpaceProperty);}
set{SetValue(ExtraSpaceProperty,value);}
}
受保护的覆盖无效数据到(ListView可绑定)
{
碱。可粘合的DTO(可粘合);
_listView=可绑定;
_listView.PropertyChanged+=(s,args)=>
{
变量计数=_listView.ItemsSource?.Cast()?.count();
if(args.PropertyName==nameof(_listView.ItemsSource)
&&count.HasValue
&&计数(数值>0)
{
int rowHeight=\u listView.rowHeight>0?\u listView.rowHeight:DefaultRowHeight;
_listView.HeightRequest=行高*计数.Value+外部空间;
}
};
}
}

示例布局显示,您在ScrollView中使用的是ListView,而ListView在不同的heig中有子级
<ScrollView>
     <ListView HasUnevenRows="True">
     </ListView>
</ScrollView>
<ScrollView>
    <StackLayout
        x:Name="DummyList"
        BindableLayout.ItemsSource="{Binding ContactList}"
        Orientation="Vertical">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <TextCell
                    Detail="{Binding StringType}"
                    DetailColor="Gray"
                    Text="{Binding Value}" />
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>
</ScrollView>