Metro Nuggets

Bitesized tidbits for building Modern (Metro) apps.

Personalisation, Cimbalino and Universal Windows Apps

With Windows 10, two key areas of personalisation for a user are the lock screen and the wallpaper images, and this is both on phone and desktop. And on both you’ve always been able to set the lock screen image (for now we’ll just forget that WPA8.1 apps couldn’t!), but setting the wallpaper was something you couldn’t do in either desktop or phone. Now that’s changing with Windows 10. Whilst the APIs for settings these aren’t the most complicated ones in the world (Wallpaper/Lock screen), no-one would ever say they wouldn’t like things even simpler, right? Also, how about being able to set these things from a viewmodel in a PCL?

The Solution

So once you’ve added the beta for the Cimbalino Toolkit with UWP support, you’ll want to go straight for the PersonalizationService (yes, it has a z, shut up!). This is a really simple service that contains two methods and a property.

Before you try and set either the lock screen or the wallpaper, you’ll need to check if this is supported (cunningly, the IsSupported property).

Both the SetLockScreenImageAsync() and SetWallpaperImageAsync() methods can be used in the same way: you pass in a path to where the image lives (it has to live locally, you can’t use external images), and you can tell the service whether the image is part of the application package (ie, came as part of your app), or if it’s been saved to a part of the app’s storage area.

private async void UIElement_OnTapped(object sender, TappedRoutedEventArgs e)
{
    var personalisation = new PersonalizationService();
    if (personalisation.IsSupported)
    {
        await personalisation.SetWallpaperImageAsync("ms-appx:///Assets/Wallpaper.jpg", true);
    }
}

The result of that is as follows:

image

Full sample of this can be found on GitHub: https://github.com/Ferret-Labs/MetroNuggetsCode/tree/master/PersonalisationService

SL

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: