microsoft / Windows-universal-samples

API samples for the Universal Windows Platform.
MIT License
9.53k stars 7.98k forks source link

Lock Screen images doesn't changing in real time #1336

Closed saudkhan214 closed 2 years ago

saudkhan214 commented 2 years ago

I was using this sample to change the lock screen images and it's working fine but the issue is that it is not changing the image in real-time. I have modified the code and added a time span of 5 sec so that images can change after 5 seconds, it is changing the image on lock screen but not in real-time.

Code `List photos = new List() { "https://images.unsplash.com/photo-1531804055935-76f44d7c3621?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8cGhvdG98ZW58MHx8MHx8&w=1000&q=80", "https://images.pexels.com/photos/556666/pexels-photo-556666.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500", "https://images.unsplash.com/photo-1530510004231-720218d936da?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTN8fHBob3RvfGVufDB8fDB8fA%3D%3D&w=1000&q=80", "https://thumbs.dreamstime.com/b/landscape-nature-mountan-alps-rainbow-76824355.jpg" }; StorageFolder tempFolder = ApplicationData.Current.LocalFolder;

        if (!Directory.Exists($"{tempFolder.Path}\\screensaver"))
            tempFolder = await tempFolder.CreateFolderAsync("screensaver");
        else
            tempFolder = await tempFolder.GetFolderAsync("screensaver");

        int count = 2;

        foreach(var photo in photos)
        {
            IRandomAccessStreamReference thumbnail =
            RandomAccessStreamReference.CreateFromUri(new Uri(photo));
            StorageFile imageFile = await StorageFile.CreateStreamedFileFromUriAsync($"photo{count}.png", new Uri(photo), thumbnail);
            try
            {
                if (imageFile != null)
                {
                    var newImgFile = await imageFile.CopyAsync(tempFolder);
                    await LockScreen.SetImageFileAsync(newImgFile);
                    // Retrieve the lock screen image that was set
                    IRandomAccessStream imageStream = LockScreen.GetImageStream();
                    await LockScreen.SetImageStreamAsync(imageStream);
                    await Task.Delay(TimeSpan.FromSeconds(5));
                }
            }
            catch(Exception ex)
            {
                await new MessageDialog(ex.Message).ShowAsync();
            }
            count = count + 1;
        }`
oldnewthing commented 2 years ago

This appears to be a question about the feature itself, not a question about the sample. You can follow the instructions on Windows developer support for support options. (Click "Forums and community".)

(Note also that UWP apps suspend when the user locks, so my guess is that the code never actually changes the image while locked.)