新手问题在这里。我有这个文件选择器:
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'的定义,并且没有扩展方法'CopyAsync'接受类型'System.Threading.Tasks.Task'的第一个参数可以找到(你是否遗漏了) using指令或程序集引用?)
答案 0 :(得分:2)
您需要在班级中设置字段或返回StorageFile
。我建议您更改PickImage()
以返回StorageFile
,以便您的代码看起来像这样;
public async StorageFile PickImage()
{
FileOpenPicker ImagePicker = new FileOpenPicker();
...
return await ImagePicker.PickSingleFileAsync(); //
...
}
private async void CreateButton_Click(object sender, RoutedEventArgs e)
{
StorageFile pickedFile = await PickImage();
StorageFile copyImage = await file.CopyAsync(DateTimeFolder, "image", NameCollisionOption.ReplaceExisting);
...
}
或者那种效果。我对CreateButton_Click
方法中的第二行感到有些困惑,因为我认为您希望对来自PickImage
的文件进行操作,而是创建一个新文件。如果您希望StorageFile
保持不变,只需将其设置为表单类上的字段并将其设置为PickImage
答案 1 :(得分:1)
正如安德烈在评论中所指出的,您的PickImage应该返回文件,因此您可以执行以下操作:
public async StorageFile PickImage()
{
FileOpenPicker ImagePicker = new FileOpenPicker();
...
StorageFile file = await ImagePicker.PickSingleFileAsync(); //
...
return file;
}
private async void CreateButton_Click(object sender, RoutedEventArgs e)
{
...
StorageFile copyImage = await this.PickImage().CopyAsync(DateTimeFolder, "image", NameCollisionOption.ReplaceExisting);
...
}
答案 2 :(得分:0)
根据两种方法的位置,有两种解决方案:
如果方法位于同一个类文件中,则可以将StorageFile file
声明为类文件中的局部变量。这样,您就可以通过CreateButton_click
功能
如果涉及交叉线程(方法在单独的线程中工作),则需要使用委托并调用它们。详细信息可通过示例here获得。