0x00.添加引用
using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Input; using Windows.Storage; using System; using Windows.System.UserProfile; using Windows.ApplicationModel; using System.Collections.ObjectModel; using Windows.UI.Xaml.Media;
0x01.选择图片
FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = PickerViewMode.Thumbnail; openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".jpeg"); openPicker.FileTypeFilter.Add(".png"); var file = await openPicker.PickSingleFileAsync();
0x02.复制所选图片并设定
if (!UserProfilePersonalizationSettings.IsSupported()) { MessageDialog md = new MessageDialog("Not Support"); md.ShowAsync(); } else { StorageFolder base_folder = await Package.Current.InstalledLocation.GetFolderAsync("Assets");//选择App安装目录下Assets文件夹作为复制目录 await file.CopyAsync(base_folder,file.Name, NameCollisionOption.ReplaceExisting);//将选择的图片复制到base_folder StorageFile copy_file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/" + file.Name)); UserProfilePersonalizationSettings setting = UserProfilePersonalizationSettings.Current; await setting.TrySetWallpaperImageAsync(copy_file);//设置为壁纸,返回true成功,false失败 await setting.TrySetLockScreenImageAsync(copy_file);//设置为锁屏,返回true成功,false失败 }
PS.如不将所选文件复制至App目录下会出现权限问题