【问题标题】:Xamarin.Forms PullToRefresh not occuring when ListView is empty?ListView 为空时不会发生 Xamarin.Forms PullToRefresh?
【发布时间】:2019-05-05 21:41:31
【问题描述】:

在 Xamarin.Forms 中,我有一个简单的 ListView,它使用 MVVM 绑定到视图模型。

  <ListView Grid.Row="1"
                     ItemsSource="{Binding ContactsGrouped}"
                     IsGroupingEnabled="true"
                     IsPullToRefreshEnabled="True"
                     RefreshCommand="{Binding RefreshCommand}"
                     IsRefreshing="{Binding IsRefreshing}"
                     GroupDisplayBinding="{Binding Key}"
                     GroupShortNameBinding="{Binding Key}"
                     BackgroundColor="Transparent"
                     SelectionMode="Single"
                     HasUnevenRows="true" 
                     SeparatorColor="#cccccc">
        <ListView.ItemTemplate>
            <cr:MyItemTemplate>
        </ListView.ItemTemplate>
    </ListView>

这是我的 xaml 代码,如果列表至少有 1 个项目,它可以正常工作。通过点击并下拉项目,列表视图可以很好地刷新,但是在项目外部点击和下拉,列表不会导致 PullToRefresh 发生。就好像 ListView 有透明的输入,但它的项目没有,因此允许它工作。

我的示例图像中的任何红色部分都显示了如果我点击并向下拖动活动指示器的区域,则该区域显示正常并且会发生刷新。然而,点击并拖动绿色的任意位置会导致视图不刷新并且活动指示器不出现。

我不会显示我的视图模型,因为问题在于视图。我还尝试将背景颜色设置为红色,检查透明是否导致问题,遗憾的是事实并非如此。有什么想法吗?

【问题讨论】:

  • 从项目外拉下来是什么意思?如果listView为空,拉动刷新时能看到Activity Indicator吗?

标签: c# xaml xamarin.forms


【解决方案1】:

我刚刚在我这边重现了您的问题。经过一番研究,我发现它是 Xamarin.forms 3.5.0 的已知问题,它仍然存在于 Latest stable 3.6.0.344457

WorkAround 将您的 Xamarin.forms 版本降级为 3.4.0

另外,我在最新的预发布版4.0.0.394984-pre10 中对其进行了测试,它运行良好。所以,我相信这个问题会在下一个版本中得到解决。

您可以关注这个issue查看过程。

【讨论】:

  • 我将 Xamarin 更新到 4.5 版,问题依旧
【解决方案2】:

尝试添加 VerticalOptions="StartAndExpand"。

<ListView Grid.Row="1"
    ItemsSource="{Binding ContactsGrouped}"
    IsGroupingEnabled="true"
    IsPullToRefreshEnabled="True"
    RefreshCommand="{Binding RefreshCommand}"
    IsRefreshing="{Binding IsRefreshing}"
    GroupDisplayBinding="{Binding Key}"
    GroupShortNameBinding="{Binding Key}"
    BackgroundColor="Transparent"
    SelectionMode="Single"
    HasUnevenRows="true" 
    SeparatorColor="#cccccc"
    VerticalOptions="StartAndExpand">
        <ListView.ItemTemplate>
            <cr:MyItemTemplate>
        </ListView.ItemTemplate>
</ListView>

这个属性告诉 View 应该覆盖多少屏幕空间。如果您不添加它,那么视图将只覆盖显示所需数据所需的任何长度。

【讨论】:

  • 这里是不是打错字了,应该是VerticalOptions吧?请说明如何将其添加到问题中给出的代码中(复制并粘贴代码并添加您的建议)并解释 VerticalOptions 属性设置的作用。这将大大改善您的答案和代表。
  • 感谢您的回复,但 ListView 确实填满了整个空间。我已经通过将它的背景设置为红色来检查它。问题是,当我在列表视图上点击并向下拖动时,不会发生刷新,但如果我点击并下拉列表视图中的某个项目,它可以正常工作。所以如果列表视图为空,我无法刷新。
  • 我认为如果列表视图没有项目拉刷新不起作用,那么您必须再次分配列表项目。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多