Joh*_*ore 3 c# xamarin.ios xamarin xamarin.forms
以下XF应用程序(下面的代码)创建一个包含2个自定义单元格的简单ListView.点击单元格使用IsVisible属性来显示第二个标签.
在Android上,这可以很好地调整ViewCell的大小以适应当前显示的内容.当Detail项目可见时,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>
Run Code Online (Sandbox Code Playgroud)
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));
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
选项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();
}
}
Run Code Online (Sandbox Code Playgroud)
请确保HasUnevenRows = true
,否则强制更新将不起作用.
归档时间: |
|
查看次数: |
2022 次 |
最近记录: |