TrySetWallpaperImageAsync()始终返回false

时间:2015-10-26 04:38:31

标签: c# windows-10 uwp

我正在尝试将壁纸设置为Windows 10设备上的图像:

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;

存储文件创建正常,尝试使用各种文件夹中的各种图像(例如我的图片,资产,LocalState);总是返回false并且没有设置壁纸?我对图片库有读/写权限,尝试在调试和发布版本中运行。显然其他人也有problem

1 个答案:

答案 0 :(得分:2)

您的应用无法从任何文件夹设置壁纸。复制ApplicationData.Current.LocalFolder中的文件并从那里设置壁纸。 我的代码:

    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;
        }