DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • A Quick Way To Build Mobile Check Capture App Using Xamarin.Forms
  • Register and Login Using SQLite in Xamarin.Forms
  • Why Use Xamarin for Mobile App Development
  • Xamarin.Forms — Working With Firebase Storage

Trending

  • Testing Swing Application
  • Optimizing Generative AI With Retrieval-Augmented Generation: Architecture, Algorithms, and Applications Overview
  • Unlocking the Power of Streaming: Effortlessly Upload Gigabytes to AWS S3 With Node.js
  • How to Integrate Istio and SPIRE for Secure Workload Identity
  1. DZone
  2. Coding
  3. Frameworks
  4. Xamarin.Forms — ListView With Pull to Refresh

Xamarin.Forms — ListView With Pull to Refresh

Learn how to set up a project allowing users to pull the screen down to refresh a list in Xamarin mobile apps.

Delpin Susai Raj user avatar by
Delpin Susai Raj
·
Jun. 16, 18 · Tutorial
Like (1)
Save
Tweet
Share
29.2K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

Image title

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

Pull to Refresh

Xamarin Forms ListView control has the ability to allow the user to pull down from the top of the ListView to trigger a refresh command. When the user triggers a PullToRefresh the Command will be invoked the Refreshed event.

Prerequisites

  • Visual Studio 2017 (Windows or Mac)

Setting Up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.

Choose the  Cross-platform App project under Visual C#-->Cross-platform in the New Project dialog.

Image title

Now Select the Blank App and Choose Portable Class Library (PCL).

Subsequently, go to the solution. In there, you get all the files and sources of your project (PCL). Now, select XAML page and double-click to open the MainPage.Xaml page.

You now have a basic Xamarin.Forms app. Click the Play button to try it out.

Setting Up the User Interface

In this step, create a simple ListView.

Now, write the following code.

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamarinListView"
             x:Class="XamarinListView.MainPage">
    <ContentPage.Content>
        <StackLayout>
            <Image x:Name="imgBanner"></Image>
            <ListView x:Name="listPlatforms" ></ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>


Now, add a list item to ListView and write the following code:

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace XamarinListView
{
    public partial class MainPage : ContentPage
    {
        List<string> listItems = new List<string>();
        public MainPage()
        {
            InitializeComponent();
            imgBanner.Source = ImageSource.FromResource("XamarinListView.images.banner.png");
            listItems.Add("Xamarin.Forms");
            listItems.Add("Xamarin.Android");
            listItems.Add("Xamarin.iOS");
            listPlatforms.ItemsSource = listItems;
        }
    }
}


Click the Play button to try it out.

Image title

Implement Pull to Refresh

In this step, you need to implement the Pull to Refresh in MainPage.Xaml.

 IsPullToRefreshEnabled="True" 

Now, write the following code:

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamarinListView"
             x:Class="XamarinListView.MainPage">
    <ContentPage.Content>
        <StackLayout>
            <Image x:Name="imgBanner"></Image>
            <ListView x:Name="listPlatforms" IsPullToRefreshEnabled="True"></ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>


In this step, add the RefreshCommand to ListView:

Command RefreshCommand = new Command();

Now, write the code given below:

MainPage.xaml.cs


 listPlatforms.RefreshCommand = new Command(() => {

                //Do your stuff.
                RefreshData();
                listPlatforms.IsRefreshing = false;
            });


Full Code — MainPage.Xaml.cs

MainPage.Xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace XamarinListView
{
    public partial class MainPage : ContentPage
    {
        List<string> listItems = new List<string>();
        public MainPage()
        {
            InitializeComponent();
            imgBanner.Source = ImageSource.FromResource("XamarinListView.images.banner.png");
            listItems.Add("Xamarin.Forms");
            listItems.Add("Xamarin.Android");
            listItems.Add("Xamarin.iOS");
            listPlatforms.ItemsSource = listItems;

            listPlatforms.RefreshCommand = new Command(() => {

                //Do your stuff.
                RefreshData();
                listPlatforms.IsRefreshing = false;
            });
        }
        public void RefreshData()
        {
            listItems.Add("Xamarin Monkeys");

            listPlatforms.ItemsSource = null;

            listPlatforms.ItemsSource = listItems;
        }
    }
}


Click the Play button to try it out.

Image title

I hope you understand how to use ListView with Pull to Refresh in Xamarin.Forms.

Thanks for reading. Please share comments and feedback.

xamarin.forms

Opinions expressed by DZone contributors are their own.

Related

  • A Quick Way To Build Mobile Check Capture App Using Xamarin.Forms
  • Register and Login Using SQLite in Xamarin.Forms
  • Why Use Xamarin for Mobile App Development
  • Xamarin.Forms — Working With Firebase Storage

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: