Call file from another method

青春壹個敷衍的年華 提交于 2020-01-17 08:27:38

问题


Newbie question here. I have this file picker:

public async void PickImage()
{
    FileOpenPicker ImagePicker = new FileOpenPicker();
    ...
    StorageFile file = await ImagePicker.PickSingleFileAsync(); // 
    ...
}

And I want to use the file set by this image picker in another method. Something like this:

private async void CreateButton_Click(object sender, RoutedEventArgs e)
{
    ...                         the one from PickImage()
                                    v
    StorageFile copyImage = await file.CopyAsync(DateTimeFolder, "image", NameCollisionOption.ReplaceExisting);
    ...
}

It is obviously not working like this. How can I do it?


Ok, based on the answers I got, this is what I came up with:

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);
    ...
}

But the last line gives me an error:

'System.Threading.Tasks.Task' does not contain a definition for 'CopyAsync' and no extension method 'CopyAsync' accepting a first argument of type 'System.Threading.Tasks.Task' could be found (are you missing a using directive or an assembly reference?)


回答1:


You need to either set a field in the class or return the StorageFile. I would suggest changing PickImage() to return the StorageFile so you're code would instead look like this;

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);
    ...
}

Or something to that effect. I'm slightly confused by the second line in your CreateButton_Click method because I thought you wanted to operate on the file from PickImage but instead you're creating a new file. If you want the StorageFile to persist just make it a field on the form class and set it in PickImage




回答2:


As pointed by Andre in the comments, your PickImage should return the file, so you could do the following:

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);
    ...
}



回答3:


Depending on the location of the two methods, there are two solutions:

  • If the methods are located in the same class file, you can declare StorageFile file as a local variable in the class file. In that way, you can reach it from the CreateButton_click function

  • If cross threading is involved (methods are working in separate threads), you need to use delegates and invoke them. Detailed information is available here through an example.



来源:https://stackoverflow.com/questions/16797452/call-file-from-another-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!