从另一个方法调用的文件

从另一个方法调用的文件

本文介绍了从另一个方法调用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 公共异步无效PickImage()
{
    FileOpenPicker ImagePicker =新FileOpenPicker();
    ...
    StorageFile文件=等待ImagePicker.PickSingleFileAsync(); //
    ...
}

和我想用在另一种方法这个图像选择器设置文件。事情是这样的:

 专用异步无效CreateButton_Click(对象发件人,RoutedEventArgs E)
{
    ......一个从PickImage()
                                    v
    StorageFile copyImage =等待file.CopyAsync(DateTimeFolder,形象,NameCollisionOption.ReplaceExisting);
    ...
}

这显然不是这样工作。我该怎么办呢?


好吧,根据我得到的答案,这是我想出了:

 公共异步任务< StorageFile> PickImage()
{
    FileOpenPicker ImagePicker =新FileOpenPicker();
    ImagePicker.FileTypeFilter.Add(JPG);
    ImagePicker.FileTypeFilter.Add(JPEG);
    ImagePicker.FileTypeFilter.Add(PNG);
    ImagePicker.ViewMode = PickerViewMode.Thumbnail;
    ImagePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    StorageFile文件=等待ImagePicker.PickSingleFileAsync();
    如果(文件!= NULL)
    {
        IRandomAccessStream的ImageStream =等待file.OpenAsync(FileAccessMode.Read);
        VAR bmpImage =新Windows.UI.Xaml.Media.Imaging.BitmapImage();
        bmpImage.De codePixelHeight = 150;
        bmpImage.De codePixelWidth = 310;
        bmpImage.SetSource(的ImageStream);
        图片preview.Source = bmpImage;
    }
    返回文件;
}////私人异步无效CreateButton_Click(对象发件人,RoutedEventArgs E)
{
    ...
    串DateTimeNow = DateTime.Now.ToString(HHmmssddMMyyyy);
    StorageFolder文档= KnownFolders.DocumentsLibrary;
    StorageFolder MYDIR =等待docs.CreateFolderAsync(我的目录,Windows.Storage.CreationCollisionOption.OpenIfExists);
    StorageFolder DateTimeFolder =等待myDir.CreateFolderAsync(DateTimeNow);
    // StorageFile图像=等待PickImage();
    StorageFile copyImage =等待PickImage()CopyAsync(DateTimeFolder,形象,NameCollisionOption.ReplaceExisting)。
    ...
}

但最后一行给我一个错误:

解决方案

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

这篇关于从另一个方法调用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 06:09