当前位置:  开发笔记 > 编程语言 > 正文

Xamarin Forms - ListView中的iOS动态ViewCell大小

如何解决《XamarinForms-ListView中的iOS动态ViewCell大小》经验,为你挑选了1个好方法。

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

在Android上,这可以很好地调整ViewCell的大小以适应当前显示的内容.当Detail项目可见时,ViewCell会展开以显示详细信息.

在iOS上,这不起作用.

以下是该应用在首次发布时的显示方式......

在此输入图像描述

当您点击第一个ViewCell时,IsVisible属性将被触发并显示详细信息项.但是,ViewCell保持相同的高度,导致它溢出,如下所示......

在此输入图像描述

如何在iOS端实现这一目标?

这是代码......

XAML

  
    
      
        
          
            
              
                
              
              
          
        
      
    
  

C#

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        ___list.ItemsSource = new List() {
                new Element() {
                    Title="First Element",
                    Detail = "First Element Details"
                },
                new Element() {
                    Title="Second Element",
                    Detail = "Second Element Details"
                }
            };
    }
}
public class Element : INotifyPropertyChanged
{
    public Element()
    {
        CellTap = new Command(() =>
        {
            ShowDetails = !ShowDetails;
        });
    }

    public ICommand CellTap { get; private set; }

    private string _title;
    public string Title
    {
        get { return _title; }
        set { if (_title != value) { _title = value; OnPropertyChanged("Title"); } }
    }
    private string _detail;
    public string Detail
    {
        get { return _detail; }
        set { if (_detail != value) { _detail = value; OnPropertyChanged("Detail"); } }
    }
    private bool _showDetails;
    public bool ShowDetails
    {
        get { return _showDetails; }
        set { if (_showDetails != value) { _showDetails = value; OnPropertyChanged("ShowDetails"); } }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Krumelur.. 5

ViewCell不能自动找出应该有多高.您必须通过设置Height或强制更新来支持它.不幸的是,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(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();
   }
}

请确保HasUnevenRows = true,否则强制更新将不起作用.



1> Krumelur..:

ViewCell不能自动找出应该有多高.您必须通过设置Height或强制更新来支持它.不幸的是,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(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();
   }
}

请确保HasUnevenRows = true,否则强制更新将不起作用.

推荐阅读
可爱的天使keven_464
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有