在运行时C#(uwp)上将图像添加到资产

时间:2018-12-09 10:26:36

标签: c# visual-studio assets windows-10-universal filepicker

我正在尝试添加图像功能。用户可以在其中上传项目的图片,并将该图片添加到我的项目资产中,以备将来使用。 这是我的代码:

private async void PickAFileButton_ClickAsync(object sender, RoutedEventArgs e)
    {
        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        openPicker.FileTypeFilter.Add(".jpg");
        openPicker.FileTypeFilter.Add(".jpeg");
        openPicker.FileTypeFilter.Add(".png");

        StorageFile file = await openPicker.PickSingleFileAsync();
        if (file != null)
        {
            // Application now has read/write access to the picked file
            String a = "ms-appx:///Assets/" + file.Name;
            theItem.Source = new BitmapImage(new Uri(a));
        }
        else
        {
            theImage.Text = "Operation cancelled.";
        }
    }

如何将给定的图片添加到项目的资产文件夹中,以便可以在侧面显示它,并将其用于其他用途?

我将非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

  

如何将给定的图片添加到项目的资产文件夹

uwp项目的资产文件夹在运行时模型中是只读的,我们无法在运行时中添加图片。我们建议使用Local文件夹替换| ?- a(3,X). X = 6 (1 ms) yes | ?- 文件夹。

Assets