Travis Liew
Travis Liew

Reputation: 797

UWP Set Lockscreen/Wallpaper from Background Task

Is there any way to set the lockscreen or wallpaper image from a Background Task? I have the following code:

if (await UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(file)) {
    Debug.WriteLine("Live wallpaper set!");
} else {
    Debug.WriteLine("Live wallpaper failed to set...");
}

Which works when I execute normally in the app, but not when I execute from a Background Task -- a breakpoint after the first line never gets hit, indicating another sync/deadlock issue (see my previous post on a similar issue). The fix in that thread also didn't work for me (it always returns false):

bool success = UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(file).GetResults(); // Try GetResults()... ALWAYS returns false

bool success = Task.Run(async () => {
    return await UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(file);
}).Result; // ALWAYS returns false...

Is there something I'm missing (maybe can't call TrySetWallpaperImageAsync from a Background Task)?

Any help would be appreciated, thanks!

Upvotes: 1

Views: 1289

Answers (1)

AbsoluteSith
AbsoluteSith

Reputation: 1967

Make sure if your if you're running a background task that its registered. And in the Run method and subsequent methods make sure to use

BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

And you don't have to use .GetResults() for TrySetWallpaperImageAsync() cause it returns a bool by default.

Upvotes: 1

Related Questions