Pull to Refresh shows how to create a simple application that shows how to implement Pull to Refresh using a RefreshContainer and ListView.
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in the Name as PullToRefresh and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10, version 1803 (10.0; Build 17134) which is the April 2018 Update and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once done select from the Menu, Project, then Add New Item…
Step 6
From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 7
Once in the Code View for Library.cs the following should be entered:
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reflection; using System.Threading.Tasks; using Windows.UI; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; namespace PullToRefresh { public class PullToRefreshData { public Brush Fill { get; set; } public string Date { get; set; } } public class Library { private readonly List _colours = typeof(Colors) .GetRuntimeProperties() .Select(c => (Color)c.GetValue(null)).ToList(); private ObservableCollection _list = new ObservableCollection(); private PullToRefreshData GetNext() { return new PullToRefreshData() { Fill = new SolidColorBrush(_colours[DateTime.Now.Second]), Date = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss") }; } private async Task FetchAsync(int count) { for (int i = 0; i < count; i++) { await Task.Delay(1000); _list.Insert(0, GetNext()); } } private async void RefreshRequested(RefreshContainer sender, RefreshRequestedEventArgs args) { using (var deferral = args.GetDeferral()) { await FetchAsync(4); } } public async void Init(RefreshContainer container, ListView display) { display.ItemsSource = _list; container.RefreshRequested += RefreshRequested; await FetchAsync(2); } public void Refresh(ref RefreshContainer container) { container.RequestRefresh(); } } }
In the Code File for Library there are using statements to include the necessary functionality. There is a PullToRefreshData Class which has Properties for Fill as Brush and string for Date.
The Library Class has Member for List of Color which is populated from all the available Colors and for ObservableCollection of PullToRefreshData. The GetNext Method will create a PullToRefreshData with the Fill set to a SolidColorBrush from one of the _colours based on the current Second and Date set to the current DateTime as string.
Also in the Library Class the FetchAsync Method will Insert into the ObservableCollection after a Delay to simulate loading more items. RefreshRequested is an Event Handler that will call the FetchAsync Method. The Init Method is used to set the ItemsSource of the ListView to the ObservableCollection of PullToRefreshData, setup the RefreshRequested Event Handler
and call FetchAsync for some initial items and the Refresh Method will call the RequestRefresh of the RefreshContainer.
Step 8
In the Solution Explorer select MainPage.xaml
Step 9
From the Menu choose View and then Designer
Step 10
The Design View will be displayed along with the XAML View and in this above the Grid element, enter the following XAML:
<Page.Resources> <DataTemplate x:Name="PullToRefreshTemplate" x:DataType="local:PullToRefreshData"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Rectangle Grid.Column="0" Margin="10" Height="100" Width="100" Fill="{x:Bind Path=Fill}"/> <TextBlock Grid.Column="1" Text="{x:Bind Path=Date}" VerticalAlignment="Center" Style="{StaticResource SubtitleTextBlockStyle}"/> </Grid> </DataTemplate> </Page.Resources>
Then while still in the XAML View between the Grid and /Grid elements, enter the following XAML:
<RefreshContainer Margin="50" Name="Container"> <ListView Name="Display" ItemTemplate="{StaticResource PullToRefreshTemplate}"/> </RefreshContainer> <CommandBar VerticalAlignment="Bottom"> <AppBarButton Icon="Refresh" Label="Refres" Click="Refresh_Click"/> </CommandBar>
Within the Page.Resources block of XAML above the Grid Element contains a DataTemplate to represent a PullToRefreshData Item which contains a Grid with two Columns, the first is a Rectangle with its Fill Property Databound to Fill of PullToRefreshData and the second Column contains a TextBlock which is Databound to the Date Property of PullToRefreshData.
Within the main Grid Element, the first block of XAML is a RefreshContainer Control which contains a ListView with its ItemTemplate set to the defined DataTemplate. The second block of XAML is a CommandBar with AppBarButton for Refresh which calls Refresh_Click.
Step 11
From the Menu choose View and then Code
Step 12
Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:
Library library = new Library(); protected override void OnNavigatedTo(NavigationEventArgs e) { library.Init(Container, Display); } private void Refresh_Click(object sender, RoutedEventArgs e) { library.Refresh(ref Container); }
There is an OnNavigatedTo Event Handler which calls the Init Method in the Library Class and Refresh_Click which calls the Refresh Method of the Library Class.
Step 13
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 14
Once the Application has started running the ListView will be populated with a couple of items, if you select the Refresh Button or Hold on the ListView itself and then Pull Down to load additional items into the ListView
Step 15
To Exit the Application select the Close button in the top right of the Application
This example shows how easy it is to implement a Pull To Refresh example using the RefreshContainer with a ListView you can use this with a list of items downloaded from the interner which is usually what this type of list is for to load new items and show that its also possible to use a Refresh Button to do the same thing if its not possible to Pull To Refresh.