Recently, James Montemagno published a blog post about the detection of first app run with Xamarin.Essentials. I thought it would be interesting to write a post about how the same would be accomplished with Uno Platform and UWP.
First Run Setting
The simplest way to detect the first run of your application is via a boolean setting flag. Uno Platform and UWP provide
ApplicationData.Current.LocalSettings to allow storing local settings for our application. To make things simple, we wrap the setting access in a class:
|public static class Settings|
|private static ApplicationDataContainer _settings = ApplicationData.Current.LocalSettings;|
|public static bool IsFirstRun|
|if (_settings.Values.TryGetValue(nameof(IsFirstRun), out var isFirstRun))|
|set => _settings.Values[nameof(IsFirstRun)] = value;|
The boolean flag defaults to
true if no value is found in settings. Now we can easily use it to check if the application is running for the first time:
|// Do something.|
|Settings.IsFirstRun = false;|
Version Tracking with Xamarin.Essentials
To get more flexibility for version tracking, there Xamarin.Essentials offers the version tracking API. Luckily, thanks to the magic of Uno Platform, we can use this API not only on mobile and desktop platforms, but on WebAssembly as well! You can install the preview Uno.Xamarin.Essentials NuGet package, and use the Xamarin.Essentials API for version tracking the same way as mentioned in James’ blog post without skipping a beat.
Launch Tracking with Uno.UI.Toolkit
If you prefer a more UWP-like API, you are in luck, as I created a pull request to add
LaunchTracker helper to the
Uno.UI.Toolkit package. Version tracking in the API is based on the Xamarin.Essentials
VersionTracking, with UWP-like semantics:
|// Display pop-up alert for first launch|
|// Display update notification for current version (1.0.0)|
|// Display update notification for current build number (2)|
I also added additional properties to track launch dates and launch count. These can be used to display recurring messages or prompts, for example for app rating.
|// Get number of times the app has been launched|
|// Get the time when the current instance of app has been launched|
|// Get the time the app has been launched last time|
Note that if you have a UWP-only app, you can still use this API by installing the
Uno.UI NuGet package, which provides the toolkit functionality for UWP.