TrySetWallpaperImageAsync() 总是返回 false [英] TrySetWallpaperImageAsync() always returns false

查看:33
本文介绍了TrySetWallpaperImageAsync() 总是返回 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

存储文件创建得很好,已尝试使用各种文件夹中的各种图像(例如,我的图片、资产、LocalState);总是返回 false 并且没有设置墙纸?我对图片库有读/写权限,曾尝试在调试和发布版本中运行.显然其他人也有这个 问题.

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.

推荐答案

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

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

这篇关于TrySetWallpaperImageAsync() 总是返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆