📜 ⬆️ ⬇️

Change desktop background and lock screen from CW / XAML UWP application


A rather interesting feature of UWP applications is that you can change the background and screen saver lock screen without any problems. What is strange is that even no warning is issued and no installation of permissions is required in the manifest (although something like the User Account Information item from the Capabilities manifest might well be required).

Under the cut description of a simple but funny trick.

As just mentioned, the background change does not require any permissions. It is enough to add an image file to the application (I added a ninja cat file - ninjacat.png) and use this snippet:

if (UserProfilePersonalizationSettings.IsSupported()) { StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///ninjacat.png")); //          ,       StorageFile localFile = await file.CopyAsync(ApplicationData.Current.LocalFolder,"ninjacat.png", NameCollisionOption.ReplaceExisting); UserProfilePersonalizationSettings settings = UserProfilePersonalizationSettings.Current; bool isSuccess = await settings.TrySetWallpaperImageAsync(localFile); } 

Two namespaces have been added to use the snippet:
')
 using Windows.System.UserProfile; using Windows.Storage; 

To set the background, we use the class UserProfilePersonalizationSettings , first checking whether the background profile change is supported. Please note that in order to use the image, it was necessary to copy it from the application directory to another available folder (I used the local application folder).

To install the image on the lock screen, in the snippet, replace the last line with:

  bool isSuccess = await settings.TrySetLockScreenImageAsync(localFile); 

By the way, it is obvious that the value of isSuccess will be true if successful, and false if unsuccessful.

It is also possible to upload a picture from the network In this case, our snippet will change to this:

  if (UserProfilePersonalizationSettings.IsSupported()) { StorageFile localFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("cat.png", CreationCollisionOption.ReplaceExisting); BackgroundDownloader downloader = new BackgroundDownloader(); DownloadOperation dl = downloader.CreateDownload(new Uri("http://az648995.vo.msecnd.net/win/2015/07/Windows_Insider_Ninjacat_Unicorn-1024x768-Desktop.png"), localFile); await dl.StartAsync(); UserProfilePersonalizationSettings settings = UserProfilePersonalizationSettings.Current; var isSuccess = await settings.TrySetWallpaperImageAsync(localFile); } 

But in this case, do not forget in the application manifest, in the possibilities of ticking "Internet (client)" and add a namespace

 using Windows.Networking.BackgroundTransfer; 

Source: https://habr.com/ru/post/271943/


All Articles