0

新手问题在这里。我有这个文件选择器:

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 指令还是汇编参考?)

4

3 回答 3

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

于 2013-05-28T17:02:26.020 回答
1

正如 Andre 在评论中指出的那样,您的 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);
    ...
}
于 2013-05-28T17:05:18.390 回答
0

根据两种方法的位置不同,有两种解决方案:

  • 如果方法位于同一个类文件中,则可以在类文件中声明StorageFile file为局部变量。这样,您可以从CreateButton_click函数中访问它

  • 如果涉及跨线程(方法在单独的线程中工作),则需要使用委托并调用它们。详细信息可通过示例在此处获得。

于 2013-05-28T17:07:20.117 回答