Xamarin.Forms - Working With RefreshView

Introduction

 
Xamarin.Forms - Working With RefreshView
 
Xamarin.Forms code runs on multiple platforms, each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 

RefreshView (Pull to Refresh)

 
RefreshView is a container control that provides a pull to refresh functionality for scrollable content. Therefore, the child of a RefreshView must be a scrollable control, such as ScrollView, CollectionView, or ListView.
 
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

 
Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself.
 
Create a new or existing Xamarin forms(.Net standard) Project. With Android and iOS Platform.
 
Xamarin.Forms - Working With RefreshView
 

Use RefreshView

 
Make sure your controls should be within ScrollView. 
  1. <RefreshView IsRefreshing="{Binding IsRefreshing}" RefreshColor="Red"    
  2.              Command="{Binding RefreshViewCommand}">    
  3.             <ScrollView>    
  4.            // Your Controls  
  5.       </ScrollView>    
  6. </RefreshView>   

Setting up the User Interface

 
Here, Add RefreshView in your View. Make sure your content should be within ScrollView.
 
MainPage.Xaml
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <Pages:XMonkeyBasePage xmlns="http://xamarin.com/schemas/2014/forms"     
  3.                        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
  4.                        xmlns:Behavior="clr-namespace:Prism.Behaviors;assembly=Prism.Forms"    
  5.                        xmlns:Pages="clr-namespace:XMonkey.Common.Pages"    
  6.                        xmlns:CustomViews="clr-namespace:XMonkey.Common.CustomViews"    
  7.                        x:Class="XMonkey.Views.BlogFeedPage">    
  8.     <NavigationPage.TitleView>    
  9.         <CustomViews:XMonkeyTitleView />    
  10.     </NavigationPage.TitleView>    
  11.     <Pages:XMonkeyBasePage.Behaviors>    
  12.         <Behavior:EventToCommandBehavior  EventName="Appearing" Command="{Binding RefreshCommand}"/>    
  13.         <Behavior:EventToCommandBehavior EventName="Disappearing" Command="{Binding CleanUpCommand}"/>    
  14.     </Pages:XMonkeyBasePage.Behaviors>    
  15.     <Pages:XMonkeyBasePage.PageContent>    
  16.     
  17.         <RefreshView IsRefreshing="{Binding IsRefreshing}"    
  18.              Command="{Binding RefreshViewCommand}">    
  19.     
  20.             <ScrollView>    
  21.     
  22.         <FlexLayout Direction="Column" Margin="0,100,0,0"    
  23.                 AlignItems="Center">    
  24.     
  25.             <Label FontSize="Large"  Text="{Binding Name}"/>    
  26.                 
  27.         </FlexLayout>    
  28.         </ScrollView>    
  29.        </RefreshView>    
  30.     </Pages:XMonkeyBasePage.PageContent>    
  31. </Pages:XMonkeyBasePage>     

Add RefreshCommand

 
Now, Add RefreshCommand for your RefreshView. This Command will perform while pull to refresh. After Refresh your data don't forget set IsRefreshing=false;
 
MainPageViewModel.cs 
  1. namespace XMonkey.ViewModels  
  2. {  
  3.     public class BlogFeedPageViewModel:BaseViewModel  
  4.     {  
  5.         #region Fields  
  6.   
  7.         private string _name;  
  8.         private bool _isRefreshing;  
  9.         private Command _refreshViewCommand;  
  10.         #endregion  
  11.  
  12.         #region Constructor  
  13.   
  14.         public BlogFeedPageViewModel()  
  15.         {  
  16.         }  
  17.  
  18.         #endregion  
  19.  
  20.         #region Properties  
  21.   
  22.         public string Name  
  23.         {  
  24.             get => _name;  
  25.             set => SetProperty(ref _name, value);  
  26.         }  
  27.   
  28.         public bool IsRefreshing  
  29.         {  
  30.             get => _isRefreshing;  
  31.             set => SetProperty(ref _isRefreshing, value);  
  32.         }  
  33.   
  34.         public Command RefreshViewCommand  
  35.         {  
  36.             get  
  37.             {  
  38.                 return _refreshViewCommand ?? (_refreshViewCommand = new Command(() =>  
  39.                 {  
  40.                     this.RefreshData();  
  41.                 }));  
  42.             }  
  43.         }  
  44.  
  45.         #endregion  
  46.  
  47.         #region Methods  
  48.   
  49.         private void RefreshData()  
  50.         {  
  51.             //Do your stuff  
  52.             this.Name = "Delpin";  
  53.             this.IsRefreshing = false;  
  54.         }  
  55.  
  56.         #endregion  
  57.   
  58.     }  
  59. }  
Click the "Play" button to try it out.
 
Xamarin.Forms - Working With RefreshView
 

Change Refresh ActivityIndicator Color

 
Now, Change Refresh ActivityIndicator Color. Set RefreshColor="Color". Check the below code:
 
MainPage.xaml
  1. <RefreshView IsRefreshing="{Binding IsRefreshing}" RefreshColor="Red"    
  2.              Command="{Binding RefreshViewCommand}">    
  3.             <ScrollView>    
  4.         <FlexLayout Direction="Column" Margin="0,100,0,0"    
  5.                 AlignItems="Center">    
  6.     
  7.             <Label FontSize="Large"  Text="{Binding Name}"/>    
  8.                 
  9.         </FlexLayout>    
  10.     </ScrollView>    
  11. </RefreshView>    
Click the "Play" button to try it out.
 
Xamarin.Forms - Working With RefreshView
 
Wow, it's working.Xamarin.Forms - Working With RefreshView
 
I hope you have understood how to use implement Pull to Refresh the Entire Page using RefreshView in Xamarin.Forms.
 
Thanks for reading. Please share your comments and feedback. Happy Coding Xamarin.Forms - Working With RefreshView