2
以下XF應用程序(下面的代碼)創建一個簡單的ListView和2個自定義單元格。點擊單元格使用IsVisible屬性顯示第二個標籤。Xamarin Forms - ListView中的iOS動態ViewCell大小
在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 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));
}
}