[UWP开发]在windows10中设置壁纸~UserProfilePersonalizationSettings

在之前的wp8.1和wp8中,微软没有公开设置壁纸的API,只有一个设置锁屏的API,但在Windows10中,微软为我们提供了设置壁纸的API:TrySetWallpaperImageAsync,他定义在Windows.System.UserProfile.UserProfilePersonalizationSettings类中。

在使用前,需要调用UserProfilePersonalizationSettings.IsSupported()来检测当前环境是否允许自定义壁纸,如果可以的话,才能进行后续操作。

在设置壁纸的时候,针对壁纸文件的位置是有要求的,不能随意指定一个地址,经测试,放在Windows.Storage.ApplicationData.Current.LocalFolder中是可以的,但之前在10240中,也可以直接通过URI把项目文件夹中的图片设为壁纸(调用方法Windows.Storage.StorageFile.GetFileFromApplicationUriAsync()),但是我在10586中测试时又发现行不通了,只能放在localfolder相关的位置。

if (UserProfilePersonalizationSettings.IsSupported() == true)
{
    await new MessageDialog("可以设置壁纸哦~").ShowAsync();
    var current = UserProfilePersonalizationSettings.Current;
    bool re = await current.TrySetWallpaperImageAsync(/*StorageFile*/);
    if (re == true)
    {
        await new MessageDialog("设置成功").ShowAsync();
    }
    else
    {
        await new MessageDialog("设置失败了").ShowAsync();
    }
}
else
{
    await new MessageDialog("不支持设置壁纸哦~").ShowAsync();
}

 

附上我的demo:设置壁纸.zip

 

posted @ 2015-12-10 16:19  DemoApp  阅读(1450)  评论(0编辑  收藏  举报