首页 > 解决方案 > 如何在 C# 中创建带有 URI 和时间输入的列表

问题描述

我正在尝试制作一个 基于输入时间设置桌面壁纸的 UWP 应用程序。正如您在图片中看到的,在选择图像后,您可以设置时间并将其添加到列表中。在指定的时间,图像将通过后台任务应用为墙纸。但我错过了重要的一步。我正在尝试找到一种方法来完成以下两件事。

如何实现我在我的代码中提到的内容?

//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)
    {
    }

标签: c#uwp

解决方案


您的代码中有一些错误:

  1. 您正在应用程序安装目录中创建文件。Windows.ApplicationModel.Package.Current.InstalledLocation是只读的,您不能在其中写入文件。有关详细信息,请参阅文件访问权限。您可以将 iamges 文件保存在ApplicationData.LocalFolder中。我相信您已经阅读了UserProfilePersonalizationSettings.TrySetWallpaperImageAsync(StorageFile) Method,但您误解了该文档上的代码。在该文档上,它只是从应用程序安装目录中获取图像文件,而不是在其中写入文件。所以,你的代码不正确。
  2. 您注册后台任务的代码不正确。在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);

推荐阅读