Sunday, February 18, 2018

UWP Tip #16 - UWP Community Toolkit - Part 13, the Loading… Control

Welcome back to my UWP Community Toolkit series. The previous five tips in the series can be found here:
Here's a quick tip about a straightforward but useful control. The UWP Community Toolkit has a Loading control to indicate that your UWP app is currently loading some content. Drop this into your Page or Control and make it visible when data is loading and you want to prevent users from interacting with the application.

I created a Page with a Grid containing a ListBox and a Loading control. The ListBox gets loaded with a list of animals during load and the Loading control has its IsLoading property set to true. This is the property you would normally toggle on and off while a long-running load process is occurring. Here's a look at the XAML:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
     <ListBox x:Name="MainItemsList"/>
    <controls:Loading x:Name="MainLoadingControl" HorizontalContentAlignment="Center" 
                       VerticalContentAlignment="Center" Background="DarkGray" Opacity="0.8">
         <StackPanel Orientation="Horizontal" Padding="8">
             <ProgressRing IsActive="True" Margin="0,0,10,0" Foreground="CadetBlue" />
             <TextBlock Text="Loading control with wait ring. Loading..." VerticalAlignment="Center" Foreground="White"/>
         </StackPanel>
     </controls:Loading>
</Grid>

Inside the Loading control, you can set any content you want to display for your users during the wait interval. I've chosen a horizontal StackPanel with a ProgressRing and TextBlock. Of course, a production app would probably move the content of this panel into a shared resource that could dynamically set the text of the TextBlock based on the current context of the wait/load period.

This is the code from the Page's code behind file to load the list items and display the loading panel.

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
     var listItems = new List<string>
     {
         "Item 1 - Dog",
         "Item 2 - Hamster",
         "Item 3 - Rat",
         "Item 4 - Cat",
         "Item 5 - Lizard",
         "Item 6 - Guinea Pig",
         "Item 7 - Snake",
         "Item 8 - Turtle",
         "Item 9 - Bird",
         "Item 10 - Gerbil",
         "Item 11 - Mouse",
         "Item 12 - Fish"
     };
    MainItemsList.ItemsSource = listItems;
    MainLoadingControl.IsLoading = true;
}

Again, best practices outside of a sample app would have these list items inside an ObservableCollection in the corresponding view model. The IsLoading property would also be bound to a boolean in your view model to be toggled on/off as necessary.

This is the end result of our basic list behind a Loading control.

UWPToolkit-LoadingControl1

It looks pretty slick for a few lines of code in a sample app. Go download the latest Toolkit in your UWP app and give it a try today. The GitHub source for the control can be found here.

Happy coding!

Technorati Tags: ,