以下 XF 应用程序(下面的代码)创建了一个带有 2 个自定义单元格的简单 ListView。点击单元格使用 IsVisible 属性显示第二个标签。
在 Android 上,这非常有效,因为 ViewCell 的大小会调整大小以适应当前显示的内容。当 Detail 项目可见时,ViewCell 展开以显示细节。
在 iOS 上,这不起作用。
这是应用首次启动时的显示方式...
当您点击第一个 ViewCell 时,会触发 IsVisible 属性并显示 Detail 项。但是,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 partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
___list.ItemsSource = new List<Element>() {
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));
}
}
最佳答案
ViewCell
无法自动找出它应该有多高。您必须通过设置其 Height
或强制其更新来支持它。遗憾的是,Height
不可绑定(bind)。
选项 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
,否则强制更新不会有效果。
关于c# - Xamarin Forms - ListView 中的 iOS 动态 ViewCell 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41769864/