新手问题在这里。我有这个文件选择器:
public async void PickImage()
{
FileOpenPicker ImagePicker = new FileOpenPicker();
...
StorageFile file = await ImagePicker.PickSingleFileAsync(); //
...
}
我想用另一种方法使用这个图像选择器设置的文件。像这样的东西:
private async void CreateButton_Click(object sender, RoutedEventArgs e)
{
... the one from PickImage()
v
StorageFile copyImage = await file.CopyAsync(DateTimeFolder, "image", NameCollisionOption.ReplaceExisting);
...
}
它显然不是这样工作的。我该怎么做?
好的,根据我得到的答案,这就是我想出的:
public async Task<StorageFile> PickImage()
{
FileOpenPicker ImagePicker = new FileOpenPicker();
ImagePicker.FileTypeFilter.Add(".jpg");
ImagePicker.FileTypeFilter.Add(".jpeg");
ImagePicker.FileTypeFilter.Add(".png");
ImagePicker.ViewMode = PickerViewMode.Thumbnail;
ImagePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
StorageFile file = await ImagePicker.PickSingleFileAsync();
if (file != null)
{
IRandomAccessStream imageStream = await file.OpenAsync(FileAccessMode.Read);
var bmpImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
bmpImage.DecodePixelHeight = 150;
bmpImage.DecodePixelWidth = 310;
bmpImage.SetSource(imageStream);
ImagePreview.Source = bmpImage;
}
return file;
}
////
private async void CreateButton_Click(object sender, RoutedEventArgs e)
{
...
string DateTimeNow = DateTime.Now.ToString("HHmmssddMMyyyy");
StorageFolder docs = KnownFolders.DocumentsLibrary;
StorageFolder myDir = await docs.CreateFolderAsync("My Dir", Windows.Storage.CreationCollisionOption.OpenIfExists);
StorageFolder DateTimeFolder = await myDir.CreateFolderAsync(DateTimeNow);
//StorageFile image = await PickImage();
StorageFile copyImage = await PickImage().CopyAsync(DateTimeFolder, "image", NameCollisionOption.ReplaceExisting);
...
}
但最后一行给了我一个错误:
“System.Threading.Tasks.Task”不包含“CopyAsync”的定义,并且找不到接受“System.Threading.Tasks.Task”类型的第一个参数的扩展方法“CopyAsync”(您是否缺少 using 指令还是汇编参考?)