4

I've scoured the internet for what would most likely be a simple solution but none seemed to have had the same problem as me.

So to explain it briefly, I'm using Xamarin Forms and I have a ListView:

<ListView x:Name="kpiList" 
          Style="{StaticResource customList}" 
          HasUnevenRows="true" 
          SeparatorVisibility="None" 
          IsPullToRefreshEnabled="true" 
          Grid.Row="1" 
          Grid.Column="0" 
          Grid.ColumnSpan="3">
</ListView>

I defined its RefreshCommand and IsRefreshing properties like this:

public partial class MainPage : ContentPage
{
    bool _isRefreshing = false;
    public bool IsRefreshing
    {
        get { return _isRefreshing; }
        set
        {
            _isRefreshing = value;
            OnPropertyChanged(nameof(IsRefreshing));
        }
    }

    public ICommand RefreshData;

    public MainPage()
    {
        RefreshData = new Command(RepopulateList);

        kpiList.RefreshCommand = RefreshData;
        kpiList.IsRefreshing = IsRefreshing;
    {

    void RepopulateList()
    {
        IsRefreshing = true;

        Controller.KPIs.Clear();
        foreach (KPIObj kpi in await 
        WebServices.GetUpdatedKPI(Controller.SavedKPIIds))
        {
            Controller.KPIs.Add(kpi);
        }

        IsRefreshing = false;
    }
}

The strange thing is that the first time I pull to refresh it works perfectly fine. But the second time I try, it never enters the block of code that the Command is set to.

It is almost as if the ListView "loses" the pointer to the command after the first pull to refresh.

Diego Rafael Souza
  • 5,241
  • 3
  • 23
  • 62
Marc Cilliers
  • 95
  • 1
  • 14
  • (Sorry for the porr english) Each call to your refresh command returns a diferent instance of Command, according your code. I guess here is the mistake. You should return only one instance of it... I suggest you change your command definition to set it as a separated method and set it at the constructor – Diego Rafael Souza Aug 15 '17 at 14:20
  • To be sure of this, surround you method body into an try block and alert the exception. I guess it's throwing an exception nowhere – Diego Rafael Souza Aug 15 '17 at 14:23
  • Thanks for the suggestion Diego, I've tried what you thought of and it seems it still does the same thing. No errors are thrown. – Marc Cilliers Aug 15 '17 at 14:42
  • What constructor are you adding those property setters to? Are you using some sort of MVVM framework or not? – Steven Thewissen Aug 15 '17 at 14:44
  • I'm not exactly using an MVVM framework although I understandably should be. I have attempted to assign the properties to the ListView in XAML with Binding though, but to the same result. – Marc Cilliers Aug 15 '17 at 14:59

1 Answers1

2

To make it work you need just to change this:

kpiList.IsRefreshing = IsRefreshing;

by

kpiList.SetBinding(ListView.IsRefreshingProperty, nameof(IsRefreshing));

In the first one you are only assigning the value of the IsRefreshing of your page property to the IsRefreshing property of the ListView. Any changes were omitted (as you noticed).

Note: If you do the binding in the XAML it should also work, but you will need to also set the BindingContext in the constructor of the MainPage.

Hope this helps.-

pinedax
  • 9,246
  • 2
  • 23
  • 30