C# Xamarin Forms-iOS动态视图ListView中的单元格大小

C# Xamarin Forms-iOS动态视图ListView中的单元格大小,c#,xamarin,xamarin.ios,xamarin.forms,C#,Xamarin,Xamarin.ios,Xamarin.forms,下面的XF应用程序(下面的代码)创建了一个带有2个自定义单元格的简单ListView。点击单元格使用IsVisible属性显示第二个标签 在Android上,ViewCell的大小可以调整以适应当前显示的内容,这非常有效。当详图项目可见时,ViewCell将展开以显示详图 在iOS上,这不起作用 以下是应用程序首次发布时的显示方式 轻触第一个ViewCell时,IsVisible属性将被触发,并显示详细信息项。但是,ViewCell的高度保持不变,导致其溢出,如下所示 如何在iOS端实现这一

下面的XF应用程序(下面的代码)创建了一个带有2个自定义单元格的简单ListView。点击单元格使用IsVisible属性显示第二个标签

在Android上,ViewCell的大小可以调整以适应当前显示的内容,这非常有效。当详图项目可见时,ViewCell将展开以显示详图

在iOS上,这不起作用

以下是应用程序首次发布时的显示方式

轻触第一个ViewCell时,IsVisible属性将被触发,并显示详细信息项。但是,ViewCell的高度保持不变,导致其溢出,如下所示

如何在iOS端实现这一点

这是代码

XAML

  <ContentPage.Content>
    <ListView x:Name="___list" Margin="50" HasUnevenRows="True">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <StackLayout>
              <StackLayout.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding CellTap}" />
              </StackLayout.GestureRecognizers>
              <Label Text="{Binding Title}" />
              <Label Text="{Binding Detail}" FontSize="30" IsVisible="{Binding ShowDetails}" />
            </StackLayout>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </ContentPage.Content>

C#

public分部类主页面:ContentPage
{
公共主页()
{
初始化组件();
___list.ItemsSource=新列表(){
新元素(){
Title=“第一个元素”,
Detail=“第一元素详细信息”
},
新元素(){
Title=“第二个元素”,
Detail=“第二元素详细信息”
}
};
}
}
公共类元素:INotifyPropertyChanged
{
公共元素()
{
CellTap=新命令(()=>
{
ShowDetails=!ShowDetails;
});
}
public ICommand CellTap{get;private set;}
私有字符串\u标题;
公共字符串标题
{
获取{return\u title;}
设置{if({u title!=value){{u title=value;OnPropertyChanged(“title”);}
}
私有字符串_详细信息;
公共字符串详细信息
{
获取{return\u detail;}
设置{if({u detail!=value){{u detail=value;OnPropertyChanged(“detail”);}
}
私人布卢展览详情;
公共图书馆展览详情
{
获取{return\u showDetails;}
设置{if(_showDetails!=value){u showDetails=value;OnPropertyChanged(“showDetails”);}
}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}

ViewCell
无法自动确定它应该有多高。您必须通过设置其
高度
或强制更新来支持它。不幸的是,
Height
是不可绑定的

选项1:如果每行的高度不同,且列表无法确定正确的高度,请使用此选项

class CustomViewCell : ViewCell
{
  protected override void OnBindingContextChanged()
  {
    base.OnBindingContextChanged();
    // Do some calculation in here to get the height you need.
    // Here we are using an example that bases the size on the result of ToString()
    string text = BindingContext.ToString();
    Height = 10 + ((int)(text[0]) - 65);
  } 
}
选项2:动态更改高度(可能是您想要的)

void SomeEventHandler(对象发送方、事件args args)
{
//让我们假设一个图像被点击。。。
var image=发送方作为映像;
//…图像在一个单元格中。
var viewCell=image.Parent.Parent作为viewCell;
//首先更改内容的高度(在本例中为图像)
如果(image.HeightRequest<250)
{
image.HeightRequest=image.Height+100;
//然后告诉单元格进行更新(注意:不需要您)
//对单元格进行子分类)
viewCell.ForceUpdateSize();
}
}
确保
haslows=true
,否则强制更新不会产生效果

class CustomViewCell : ViewCell
{
  protected override void OnBindingContextChanged()
  {
    base.OnBindingContextChanged();
    // Do some calculation in here to get the height you need.
    // Here we are using an example that bases the size on the result of ToString()
    string text = BindingContext.ToString();
    Height = 10 + ((int)(text[0]) - 65);
  } 
}
void SomeEventHandler(object sender, EventArgs args)
{
   // Let's assume an image was tapped...
   var image = sender as Image;
   // ...and the image is in a cell.
   var viewCell = image.Parent.Parent as ViewCell;

   // You would FIRST change the height of the content (in this case the image)
   if (image.HeightRequest < 250)
   {
       image.HeightRequest = image.Height + 100;
       // And THEN tell the cell to update (Note: you should not be required
       // to subclass the cell)
       viewCell.ForceUpdateSize();
   }
}