TrySetWallpaperImageAsync() always returns false

时光毁灭记忆、已成空白 提交于 2020-01-06 02:44:06

问题


I'm trying to set the wallpaper to an image on my Windows 10 device:

var fileName = postInf.title + ".jpg";
BitmapImage img = new BitmapImage();

bool success = false;
if (UserProfilePersonalizationSettings.IsSupported())
{
    // read from pictures library
    var pictureFile = await KnownFolders.PicturesLibrary.GetFileAsync(fileName);
    using (var pictureStream = await pictureFile.OpenAsync(FileAccessMode.Read))
    {
        img.SetSource(pictureStream);
    }

    UserProfilePersonalizationSettings profileSettings = UserProfilePersonalizationSettings.Current;
    success = await profileSettings.TrySetWallpaperImageAsync(pictureFile);
} 
return success;

The storagefile is created fine, have tried with various images from various folders (e.g. My Pictures, Assets, LocalState); always returns false and wallpaper is not set? I have read/write permissions to pictures library, have tried running in debug and release versions. Apparently others are also having this problem.


回答1:


Your app can't set wallpapers from any folder. Copy file in ApplicationData.Current.LocalFolder and set wallpaper from there. My code:

    if (list.SelectedIndex != -1)
    {
      var data = list.SelectedItem as ThumbItem;
      StorageFile newFile = await data.File.CopyAsync(ApplicationData.Current.LocalFolder);
      await SetWallpaperAsync(newFile);
    }

async Task<bool> SetWallpaperAsync(StorageFile fileItem)
        {
            bool success = false;
            if (UserProfilePersonalizationSettings.IsSupported())
            {
                UserProfilePersonalizationSettings profileSettings = UserProfilePersonalizationSettings.Current;
                success = await profileSettings.TrySetWallpaperImageAsync(fileItem);
            }
            return success;
        }


来源:https://stackoverflow.com/questions/33338571/trysetwallpaperimageasync-always-returns-false

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!