gpt4 book ai didi

c# - 如何在 C# 中创建带有 URI 和时间输入的列表

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:18 24 4
gpt4 key购买 nike

I am trying to make a UWP app 根据输入时间设置桌面墙纸。正如您在图片中看到的,选择图像后,您可以设置时间并将其添加到列表中。在指定的时间,图像将通过后台任务应用为墙纸。但是我错过了重要的一步。我正在尝试找到一种方法来完成以下两件事。

  • 我想在文件中创建一个列表,应用程序可以在其中添加图像URI 和显示时间。以及后台任务读取的方式它。
  • UI 可以显示列表中添加的图像,并提供删除其中一张的选项。

如何实现我在 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)
{
}

最佳答案

您的代码中存在一些错误:

  1. 您正在应用程序安装目录中创建文件。 Windows.ApplicationModel.Package.Current.InstalledLocation 是只读的,您不能在其中写入文件。参见 File access permissions更多细节。您可以将 iamges 文件保存在 ApplicationData.LocalFolder 中.我相信您已经阅读了 UserProfilePersonalizationSettings.TrySetWallpaperImageAsync(StorageFile) Method ,但您误解了该文档上的代码。在该文档上,它只是从应用程序安装目录获取图像文件,而不是在其中写入文件。所以,您的代码不正确。
  2. 您注册后台任务的代码不正确。代码和Package.appxmanifest中的TaskEntryPoint应该是“BackgroundTaskComponent.BackgroundClass”。你最好阅读 Create and register an out-of-process background task仔细记录,然后您就可以开始在您的应用中使用后台任务。

然后,让我们回到你最初的问题:

I want to create a list in a file in which the app can add the Image URI and time of display. and a way for the Background task to read it.

正如@Rufus L 所说,您可以定义一个类来将它们放在一起。然后,您可以将其保存在保存在 ApplicationData.LocalFolder 中的文件中。当后台任务被触发时,您可以从ApplicationData.LocalFolder 中获取它。有一种简单的方法可以保存复杂的对象。你可以使用 LocalObjectStorageHelper Windows 社区工具包的类。

代码如下所示:

 var uri = new Uri("ms-appdata:///local/TimeWallpaper/" + assetsFileName);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
UserProfilePersonalizationSettings profileSettings = UserProfilePersonalizationSettings.Current;
await profileSettings.TrySetWallpaperImageAsync(file);

关于c# - 如何在 C# 中创建带有 URI 和时间输入的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51028781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com