如何在 Xamarin Forms 的滚动视图中检测击中网格底部

How to detect hitting the bottom of a grid within a scrollview in Xamarin Forms

我正在使用 Xamarin.Forms 在网格(在 ScrollView 中)中显示来自 Websource 的项目。

当用户点击网格底部时,我想加载更多项目并将它们添加到网格中。我知道通常 ListView 更适合以这种方式显示数据(并且 ListView 有一个 ItemAppearing 事件)但遗憾的是我必须使用网格。

如果有帮助,我可以 post 一些代码,但我不确定这里是否有必要。

提前致谢 编辑:这是我非常无聊的布局文件:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyProject.MyNamespace.LayoutName"
             SizeChanged="OnPageSizeChanged">
  <ScrollView >
    <AbsoluteLayout x:Name="myLayout">
      <Grid x:Name="GridForItems" RowSpacing="6" ColumnSpacing="6">
      </Grid>
    </AbsoluteLayout>
  </ScrollView>
</ContentPage>

我以编程方式添加所有行、列和项目。

当你有一个项目列表时,你应该使用 ListView 来显示它。在此 ListView 的 DataTemplate 中,您可以指定如何显示每个项目(如果确实需要网格,请在此处使用网格)。

在Listview中,可以实现如下代码检测底部:

myListview.ItemAppearing += (sender, e) =>
{
    // CHECK HERE
    if(e.Item.Id == MyItems[MyItems.Count - 1].Id)
    {
        // YOU HIT THE BOTTOM
    } 
}

为什么要使用Listview?看看this forum thread about Dynamic Grids.

因为我没有对我的原始问题(如何检测触及 scrollview 的底部)得到令人满意的答案,而且我还没有在网络上的其他任何地方找到解决方案我要 post 我是如何解决这个问题的。

请注意,这可能不是一个优雅的解决方案,如果您有更好的答案,请post它,我会将其作为最佳答案。

我所做的是为 ScrollView 的滚动事件实现一个委托。

myScrollView.Scrolled += (sender, e) => { onScrolled(); };

在我的 onScrolled 中,我有以下 if 子句来确定我是否触及了 ScrollView 的底部:

 private void onScrolled()
 {
    if(myScrollView.ScrollY >= myScrollView.ContentSize.Height - myScrollView.Height)
    {
      //Handle hitting the bottom
    }
 }