您应该使用 FooterTemplate 和 Footer 属性。它作为 ItemTemplate 和 ItemSource:
<ListView Footer="{Binding IsFooterVisible}">
<ListView.FooterTemplate>
<DataTemplate>
<!-- Footer content. Always visible -->
</DataTemplate>
<ListView.FooterTemplate>
</ListView>
并绑定到页脚属性一些可以为空的东西(例如对象)。或者你可以使用转换器来转换:true -> new object() and false -> null
还可以创建 ListView 的子类。我的例子(IsLoading 属性就是你要搜索的):
Xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Panor.Views.LoadableListView"
x:Name="element"
>
<ListView.FooterTemplate>
<DataTemplate>
<ContentView>
<ActivityIndicator IsRunning="true"
Margin="0, 5"
Color="{StaticResource mainColor}"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
>
<ActivityIndicator.Scale>
<OnPlatform x:TypeArguments="x:Double" iOS="2" Android="1" />
</ActivityIndicator.Scale>
</ActivityIndicator>
</ContentView>
</DataTemplate>
</ListView.FooterTemplate>
</ListView>
代码隐藏:
public partial class LoadableListView : ListView
{
public LoadableListView()
{
InitializeComponent();
this.ItemAppearing += OnItemAppearing;
}
public static readonly BindableProperty IsLoadingProperty = BindableProperty.Create(
nameof(IsLoading),
typeof(bool),
typeof(LoadableListView),
false,
propertyChanged: (bindable, oldValue, newValue) =>
{
var element = (LoadableListView)bindable;
element.Footer = (bool)newValue ? new object() : null;
}
);
public bool IsLoading
{
set => SetValue(IsLoadingProperty, value);
get => (bool)GetValue(IsLoadingProperty);
}
public static readonly BindableProperty ScrolledDownCommandProperty = BindableProperty.Create(
nameof(ScrolledDownCommand),
typeof(ICommand),
typeof(LoadableListView)
);
public ICommand ScrolledDownCommand
{
set => SetValue(ScrolledDownCommandProperty, value);
get => (ICommand)GetValue(ScrolledDownCommandProperty);
}
void OnItemAppearing(object sender, ItemVisibilityEventArgs e)
{
if (ItemsSource == null) return;
if (ScrolledDownCommand == null) return;
object last = null;
if (ItemsSource is IList)
{
var length = (ItemsSource as IList).Count;
last = (ItemsSource as IList)[length - 1];
}
else
{
foreach (var item in ItemsSource)
last = item;
}
if (e.Item == last && ScrolledDownCommand.CanExecute(null))
ScrolledDownCommand.Execute(null);
}
消费:
<views:LoadableListView ItemsSource="{Binding ItemSource}"
RowHeight="120"
SeparatorColor="#c7c8c9"
IsLoading="{Binding IsMoreLoading}"
ScrolledDownCommand="{Binding ScrolledDownCommand}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsRefreshing}"
RefreshCommand="{Binding RefreshCommand}"
>
<views:LoadableListView.ItemTemplate>
<DataTemplate>
<cells:MagazinesListCell Name="{Binding Name}"
Publisher="{Binding Publisher}"
Price="{Binding Price}"
Image="{Binding Converter={StaticResource UriToImageSourceConvertor}, Path=Image}"
Commands="{Binding Commands}"
/>
</DataTemplate>
</views:LoadableListView.ItemTemplate>
</views:LoadableListView>