Reputation: 36
Can someone tell me what's wrong with my code? I'm trying to get the pull to refresh to work on my xaml page and it will not work for Android or iPhone. I think it may be how I have my listview laid out. Can anyone tell me if there is something within the ListView that may be preventing the Refresh Command to fire? Here is my xaml code for the ListView
<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>
And here are the methods in code behind...
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();
}
}
}
Thanks!
Upvotes: 0
Views: 3275
Reputation: 9356
You are not setting the BindingContext for your XAML. You need this so the XAML knows when you are using Bindings in your case in the ListView for the RefreshCommand and the Refreshing.
To do this just add this line of code in you MainPage constructor BindingContext = this;
Something like:
public MainPage()
{
InitializeComponent();
BindingContext = this;
TDRView.ItemsSource = collection;
GetTDRs();
}
this
means that the code behind will serve as the binding context of your XAML.
Here is one Example of pull to refresh..
https://xamarinhelp.com/pull-to-refresh-listview/
Hope this helps.-
Upvotes: 1
Reputation: 1795
pay attention to the OS where you are testing / using the pull to refresh.
pull-to-refresh is not supported on Windows Phone 8.1. On Windows phone 8, pull-to-refresh is not a native platform feature, so an implementation of pull-to-refresh is provided by Xamarin.Forms.
Finally, be aware that pull-to-refresh will not work on Windows Phone if all elements in the list can fit on the screen (in other words, if vertical scrolling isn't required).
Upvotes: 0