🕓 9 MIN Ever wondered how to …
In this blog post, I will show you how to create a simple skeleton loader for your Uno Platform applications. The Skeleton loader is a method of loading content in mobile apps that utilizes a visual placeholder user interface resembling the shape of the loaded content. This approach significantly contributes to enhancing the user experience by reducing perceived load times.
In this tutorial, we will guide you through the process of incorporating a Pulsing Placeholder. This involves smoothly transitioning the opacity of the skeleton objects, creating a pulsating effect as they fade in and out.
To create a skeleton loader, we will use the ColorAnimation and Storyboard classes from the WinUI library, which are available on all platforms supported by Uno Platform. These classes allow us to animate the color of a UI element over time, creating a pulsing effect that simulates the loading process.
The first step is to create an Uno Platform User Control XAML file that defines the layout of our skeleton loader. For this example, we will create a simple LoaderControl.xaml that consists of a rectangle, and in the code behind, we will call the Animation begin method.
Here is the XAML code:
The XAML code above creates a Color Animation that will change the rectangle’s color to Dark-Gray within the first 2 seconds, leveraging the LinearColorKeyFrame, creating a smooth linear animation between values. The next 0.5 seconds will have the rectangle’s color jump from the Dark-Gray starting color to #7f7f7f7f, and this is achieved via the use of the DiscreteColorKeyFrame. Finally, the last 2 seconds will have the animation leverage the SplineColorKeyFrame to create a variable transition between #7f7f7f7f and Dark-Gray.
In this example, the animation starts slow but speeds up exponentially toward the end of the time segment. We will set the Duration property to 2 seconds and the AutoReverse property to true so that the animation will repeat indefinitely. We will also set the EnableDependentAnimation property to true, which some platforms need to run color animations.
And the code behind is shown below:
public sealed partial class LoaderControl : UserControl
{
public LoaderControl()
{
this.InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
LoaderAnimation.Begin();
}
}
The next step is to create a ColorAnimation that will change the fill color of the ImageRect and the foreground color of the text blocks from light gray (#EEEEEE) to dark gray (#CCCCCC) and back. We will set the Duration property to 2 seconds and the AutoReverse property to true so that the animation will repeat indefinitely. We will also set the EnableDependentAnimation property to true, which some platforms need to run color animations. Here is the code:
The final step is to create 2 data templates; Loading and Loaded Templates which will serve as the Item Template based on the boolean isLoading Property from the view model.
Here is the code:
And the code behind is shown below:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void itemsListView_Tapped(object sender, Microsoft.UI.Xaml.Input.TappedRoutedEventArgs e)
{
var listView = sender as ListView;
var viewModel = listView?.DataContext as BindableMainModel;
if (viewModel != null)
{
if (viewModel.IsLoading)
{
listView.ItemTemplate = (DataTemplate)Resources["LoadingTemplate"];
}
else
{
listView.ItemTemplate = (DataTemplate)Resources["LoadedTemplate"];
}
viewModel.IsLoading = !viewModel.IsLoading;
}
}
}
That’s all there is to it! We’ve successfully developed a basic skeleton loader for Uno Platform apps using WinUI. Take a look at the video below to see the end result running on Windows and WebAssembly.
I hope you enjoyed this blog post and learned something new. If you have any questions or feedback, please let us know.
Thank you for reading!
For those new to the Uno Platform, it allows for creating pixel-perfect, single-source C# and XAML apps that run natively on Windows, iOS, Android, macOS, Linux and Web via WebAssembly. In addition, it offers Figma integration for design-development handoff and a set of extensions to bootstrap your projects. Uno Platform is free, open-source (Apache 2.0), and available on GitHub.
To upgrade to the latest release of Uno Platform, please update your packages to 4.9 via your Visual Studio NuGet package manager! If you are new to Uno Platform, following our official getting started guide is the best way to get started. (5 min to complete)
Tags:
🕓 9 MIN Ever wondered how to …
🕓 3 MIN State management patterns like …
🕓 3 MIN We are introducing over …
Uno Platform
360 rue Saint-Jacques, suite G101,
Montréal, Québec, Canada
H2Y 1P5
USA/CANADA toll free: +1-877-237-0471
International: +1-514-312-6958
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
Uno Platform 5.2 LIVE Webinar – Today at 3 PM EST – Watch