Martin Suchan – BloQ Random #WPdev stuff

11Jul/153

New API for changing Start screen background image in Windows 10 v10166

Windows 10 Insider Preview v10166 was just recently released for PCs and Phones, together with related SDK.

In this short article I'll show you how to use new API for changing Desktop/Start screen background image, that is first available in this version.

 

The API is located in new class UserProfilePersonalizationSettings in the Windows.SystemUserProfile namespace. It has currently only few members:

public sealed class UserProfilePersonalizationSettings : IUserProfilePersonalizationSettings
{
    public IAsyncOperation<bool> IUserProfilePersonalizationSettings.TrySetLockScreenImageAsync(StorageFile imageFile);
    public IAsyncOperation<bool> IUserProfilePersonalizationSettings.TrySetWallpaperImageAsync(StorageFile imageFile);
    public static bool IsSupported();
    public static UserProfilePersonalizationSettings Current { get; }
}

For changing the background image you just need to get StorageFile of your image and call TrySetWallpaperImageAsync and that's all. Complete code sample could look like this. Note I have placed newbg.jpg image in the root folder of my project and set the compile option to Content:

private static async Task ChangeStartWallpaper()
{
    if (UserProfilePersonalizationSettings.IsSupported())
    {
        StorageFile newBg = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///newbg.jpg"));
        UserProfilePersonalizationSettings ups = UserProfilePersonalizationSettings.Current;
        await ups.TrySetWallpaperImageAsync(newBg);
    }
}

I've tested this API just now and it works both on phone and PC. Strangely it works immediately, no question asked. There should probably be some kind of dialog or settings that only selected app can change these image. I can imagine two apps fighting to change the background images in background tasks.

That's all for now. I really like this simple new API that will make a great addition to my Windows and Windows Phone apps Astronomy Image of the Day that already can change the lockscreen image with daily astro photo.

  • Lee McPherson

    Do you have something special set in the manifest? Because this does not work for me yet. I’ve updated the SDK and updated the build targets on all my projects, too. I’ve been trying to use this since the last build 🙁

    • Lee McPherson

      Very interesting. It works if set from the ms-appx uri, but not if the image is located in the LocalCache folder. I will try other appdata folders

      • Lee McPherson

        Ok, it works from LocalFolder, but not LocalCacheFolder. Perhaps the temporary nature of this folder (and maybe TemporaryFolder) is what is preventing this from working.