c# - 如何在 C# 中创建带有 URI 和时间输入的列表
问题描述
我正在尝试制作一个 基于输入时间设置桌面壁纸的 UWP 应用程序。正如您在图片中看到的,在选择图像后,您可以设置时间并将其添加到列表中。在指定的时间,图像将通过后台任务应用为墙纸。但我错过了重要的一步。我正在尝试找到一种方法来完成以下两件事。
- 我想在应用程序可以添加图像 URI 和显示时间的文件中创建一个列表。以及后台任务读取它的方法。
- UI 可以显示列表中添加的图像,并可以选择删除其中一个。
如何实现我在我的代码中提到的内容?
//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)
{
}
解决方案
您的代码中有一些错误:
- 您正在应用程序安装目录中创建文件。
Windows.ApplicationModel.Package.Current.InstalledLocation
是只读的,您不能在其中写入文件。有关详细信息,请参阅文件访问权限。您可以将 iamges 文件保存在ApplicationData.LocalFolder中。我相信您已经阅读了UserProfilePersonalizationSettings.TrySetWallpaperImageAsync(StorageFile) Method,但您误解了该文档上的代码。在该文档上,它只是从应用程序安装目录中获取图像文件,而不是在其中写入文件。所以,你的代码不正确。 - 您注册后台任务的代码不正确。在
TaskEntryPoint
代码中,Package.appxmanifest
应该是“BackgroundTaskComponent.BackgroundClass”。你最好仔细阅读创建和注册进程外后台任务文档,然后你就可以开始在你的应用程序中使用后台任务了。
然后,让我们回到你原来的问题:
我想在应用程序可以添加图像 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);
推荐阅读
- android - 反应原生:重启手机后应用程序消失
- arrays - 为什么无法从 reactjs 中的 json 响应中读取数组子元素?
- c++ - 文件名后的 Clang 关键字
- css - Material-UI 中的 JssProvider 没有将我的自定义生产前缀应用于 CSS
- android - 再次按返回退出 android studio
- ruby-on-rails - 活动管理导轨中的分页计数问题
- c++ - 如何 C++ 编译静态第三方库,其中包含其他人在主应用程序中使用的另一个库
- unity3d - 我需要白色的碎片在马路的顶部,而不是在马路后面
- c++ - 带有表达式模板的多维数组模板类
- apache-spark - spark中用于添加新列的withcolumn()未显示结果