【问题标题】:Pull To Refresh not working in Xamarin Forms ListView拉动刷新在 Xamarin 表单 ListView 中不起作用
【发布时间】:2017-10-20 19:19:32
【问题描述】:

谁能告诉我我的代码有什么问题?我正在尝试让刷新以在我的 xaml 页面上工作,但它不适用于 Android 或 iPhone。我认为这可能是我的列表视图布局方式。谁能告诉我 ListView 中是否有可能阻止刷新命令触发的东西?这是我的 ListView 的 xaml 代码

 <ContentPage.Content>
    <ListView x:Name="TDRView" HasUnevenRows="True" ItemTapped="OnItemTapped" IsPullToRefreshEnabled="True" RefreshCommand="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <Grid x:Name ="gridTDR">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="50*" />
                                <ColumnDefinition Width="25*" />
                                <ColumnDefinition Width="25*" />
                            </Grid.ColumnDefinitions>
                            <StackLayout HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand" Grid.Column="0" Grid.Row="0">
                                <Label x:Name="lblTDRID" Text="{Binding TDRID}" IsVisible="false" />
                                <Label x:Name="lblCustName" Text="{Binding CustomerName}" FontSize="Medium" TextColor="Black" />
                            </StackLayout>
                            <Button Image="iconApprove.png" Clicked="OnApproveButtonClicked" Grid.Column="1" Grid.Row="0" VerticalOptions="Center" BackgroundColor="Transparent" />
                            <Button Image="iconReject.png" Clicked="OnRejectButtonClicked" Grid.Column="2" Grid.Row="0" VerticalOptions="Center" BackgroundColor="Transparent" />
                        </Grid>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>

下面是代码中的方法...

    using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;

namespace AppNameSpace
{
    public partial class MainPage : ContentPage
    {
        ObservableCollection<TDR> collection = new ObservableCollection<TDR>();

        private bool _isRefreshing = false;
        public bool IsRefreshing
        {
            get { return _isRefreshing; }
            set
            {
                _isRefreshing = value;
                OnPropertyChanged(nameof(IsRefreshing));
            }
        }

        public ICommand RefreshCommand
        {
            get
            {
                return new Command(async () =>
                {
                    IsRefreshing = true;
                    await Service.GetPendingTDRs();
                    IsRefreshing = false;
                });
            }
        }

        public MainPage()
        {
            InitializeComponent();
            TDRView.ItemsSource = collection;
            GetTDRs();
        }
     }
   }

谢谢!

【问题讨论】:

  • 您是否真的使用调试器来验证刷新 cmd 是否正在触发?
  • 是的,我已经调试并设置了断点,它不会可怕
  • 当您说“它不工作”时,您的意思是在 iOS 中,例如,当拉动列表时,指示器出现并开始旋转,但它继续像这样继续并且列表永远不会刷新?您还可以更新您的帖子,显示您的完整代码吗?
  • 没错。我只得到纺车。如果后面的代码发生任何事情,它不会出现。
  • 我已经把剩下的代码贴在后面了。谢谢

标签: .net xaml xamarin xamarin.forms cross-platform


【解决方案1】:

注意您正在测试/使用拉刷新的操作系统。

Windows Phone 8.1 不支持下拉刷新。 在 Windows phone 8 上,拉动刷新不是本机平台功能,因此 Xamarin.Forms 提供了拉动刷新的实现。

最后,请注意,如果列表中的所有元素都适合屏幕(换句话说,如果不需要垂直滚动),则下拉刷新将无法在 Windows Phone 上工作。

https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/interactivity/#Pull_to_Refresh

【讨论】:

    【解决方案2】:

    您没有为 XAML 设置 BindingContext。您需要这个,以便 XAML 知道您何时在 ListView 中为 RefreshCommand 和 Refreshing 使用绑定。

    要做到这一点,只需在 MainPage 构造函数中添加这行代码BindingContext = this;

    类似:

    public MainPage()
    {
        InitializeComponent();
        BindingContext = this;
        TDRView.ItemsSource = collection;
        GetTDRs();
    }
    

    this 表示后面的代码将用作 XAML 的绑定上下文。

    这是一个拉动刷新的例子..

    https://xamarinhelp.com/pull-to-refresh-listview/

    希望这会有所帮助。-

    【讨论】:

    • 做到了!谢谢!
    猜你喜欢
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 2016-02-03
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    相关资源
    最近更新 更多