I am trying to make a UWP app 根据输入时间设置桌面墙纸。如您在图片中看到的,选择图像后,您需要设置时间并将其添加到列表中。在指定的时间,图像将通过后台任务作为墙纸应用。但是我错过了重要的一步。我正在尝试找到一种方法来完成以下两件事。
如何实现在the code I have中提到的内容?
//Pickup Image file
private async void FilePickerWallpaper(object sender, RoutedEventArgs e)
{
FileOpenPicker pickerWallpaper = new FileOpenPicker();
pickerWallpaper.ViewMode = PickerViewMode.Thumbnail;
pickerWallpaper.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
pickerWallpaper.FileTypeFilter.Add(".jpg");
pickerWallpaper.FileTypeFilter.Add(".jpeg");
pickerWallpaper.FileTypeFilter.Add(".png");
//Get to the App local folder
StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// Create a subfoloder
String timeNameFile = "TimeWallpaper";
StorageFolder timeFolder = await appFolder.CreateFolderAsync(timeNameFile, CreationCollisionOption.OpenIfExists);
//Check if the folder was created
if (await appFolder.TryGetItemAsync(timeNameFile) != null) Debug.WriteLine("Folder" + timeNameFile + "exist");
else Debug.WriteLine("Folder" + timeNameFile + "does not exist");
//Pick an Image
StorageFile fileName = await pickerWallpaper.PickSingleFileAsync();
if (fileName != null)
{
//Check if the file does not exist
if (await timeFolder.TryGetItemAsync(fileName.Name) != null)
{
string selectedImgName = fileName.Name;
await fileName.CopyAsync(timeFolder, selectedImgName, NameCollisionOption.ReplaceExisting);
//Preview the Image on the interface
selectImg.Source = new BitmapImage(new Uri("ms-appx:///TimeWallpaper/" + selectedImgName));
}
else
{
selectImg.Source = new BitmapImage(new Uri("ms-appx:///Assets/wallpaper.png"));
}
}
}
//add selected file to the List - w
private void AddFile00(object sender, RoutedEventArgs e)
{
BitmapImage bitImageSource = (BitmapImage)selectImg.Source;
}
//Oops! this is to remove the last added image to the list
private void RemoveFile(object sender, RoutedEventArgs e)
{
}
答案 0 :(得分:1)
您的代码中有一些错误:
Windows.ApplicationModel.Package.Current.InstalledLocation
是只读的,您不能在其中写入文件。有关更多详细信息,请参见File access permissions。您可以将iamges文件保存在ApplicationData.LocalFolder中。我相信您已经阅读了UserProfilePersonalizationSettings.TrySetWallpaperImageAsync(StorageFile) Method,但是您误解了该文档上的代码。在该文档上,它只是从应用程序安装目录获取映像文件,而不是在其中写入文件。因此,您的代码不正确。TaskEntryPoint
和Package.appxmanifest
应该是“ BackgroundTaskComponent.BackgroundClass”。您最好仔细阅读Create and register an out-of-process background task文档,然后才能开始在应用程序中使用后台任务。然后,让我们回到您的原始问题:
我想在文件中创建一个列表,应用程序可以在其中添加图像URI和显示时间。以及后台任务读取它的方式。
正如@Rufus L所说,您可以定义一个类来将它们保持在一起。然后,您可以将其保存在ApplicationData.LocalFolder
中保存的文件中。触发后台任务后,您可以从ApplicationData.LocalFolder
获取它。有一种保存复杂对象的简单方法。您可以使用Windows社区工具包的LocalObjectStorageHelper类。
代码如下:
var uri = new Uri("ms-appdata:///local/TimeWallpaper/" + assetsFileName);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
UserProfilePersonalizationSettings profileSettings = UserProfilePersonalizationSettings.Current;
await profileSettings.TrySetWallpaperImageAsync(file);