- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的代码到底出了什么问题?
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker FilePicker = new FileOpenPicker();
FilePicker.FileTypeFilter.Add(".exe");
FilePicker.ViewMode = PickerViewMode.List;
FilePicker.SuggestedStartLocation = PickerLocationId.Desktop;
// IF I PUT AWAIT HERE V I GET ANOTHER ERROR¹
StorageFile file = FilePicker.PickSingleFileAsync();
if (file != null)
{
AppPath.Text = file.Name;
}
else
{
AppPath.Text = "";
}
}
它给我这个错误:
Cannot implicitly convert type 'Windows.Foundation.IAsyncOperation' to 'Windows.Storage.StorageFile'
如果我添加 'await',就像在代码中评论的那样,我会收到以下错误:
¹ The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
代码源here
最佳答案
好吧,编译器错误消息非常直接地解释了您的代码无法编译的原因。 FileOpenPicker.PickSingleFileAsync
返回 IAsyncOperation<StorageFile>
- 所以不,您不能将该返回值分配给 StorageFile
多变的。典型使用方式IAsyncOperation<>
在 C# 中是 await
.
您只能使用 await
在async
方法...所以您可能想将方法更改为异步方法:
private async void BrowseButton_Click(object sender, RoutedEventArgs e)
{
...
StorageFile file = await FilePicker.PickSingleFileAsync();
...
}
请注意,对于除事件处理程序之外的任何其他内容,最好让异步方法返回 Task
。而不是 void
- 使用 void
的能力实际上只是为了让您可以使用异步方法作为事件处理程序。
如果您不是很熟悉 async
/await
然而,您可能应该在继续之前阅读它 - MSDN "Asynchronous Programming with async and await"页面可能是一个不错的起点。
关于c# - 无法将类型 IAsyncOperation<StorageFile> 隐式转换为 StorageFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16512346/
我的代码到底出了什么问题? private void BrowseButton_Click(object sender, RoutedEventArgs e) { Fi
我需要相应地按字节 block 写入文件数据(在我的 Metro 应用程序中)并且有一个类 FileIO 具有方法 AppendTextAsync 和 WriteBytesAsync 但不需要 App
我在 WinRT 工作,但我有点卡住了: 我正在编写具有媒体库功能的音乐播放器。我在 SQLite 数据库中保存有关音乐的信息(例如艺术家等)。我想让用户将他的音乐保存在他想要的任何地方,而不是 Wi
我有一个 StorageFile 对象,我正在尝试读入字符串(用于调试目的)。最简单的方法是什么? 最佳答案 在 C# 中: string text = await Windows.Storage.F
我正在尝试写入一个文件,但“偶尔”我遇到了我认为归结为并发性的问题,因为有些的时候,我得到了一个系统.UnauthorizedAccessException 消息: Access is denied.
我正在尝试完成一项本应非常简单的任务,即按照修改日期的降序从 StorageFolder 中获取 StorageFile 的列表。 检索列表但未排序的当前代码 Task
我遇到了一些关于 FileSavePicker 的问题。是否有关于保存 StorageFile 而不显示任何弹出窗口或对话框询问用户的解决方案。我想从后面的代码中给出存储文件的当前路径。 var by
在 Win8 中,我使用 somefile.CopyAsync(destFolder, newName) 来复制文件。但是当我尝试取消它时,它似乎不起作用。这两种方法是我尝试取消它的方式: 只需使用
我需要能够从 UWP 应用程序中的路径访问文件。 该文件首先由用户使用 OpenFilePicker 选择。然后我需要将此文件传递给 FFmpeg 包装器,该包装器从文件路径设置其输入。目前,对于应用
在这里快速搜索一无所获。因此,我已经开始使用一些相当迂回的方式在我的 WinRT 应用程序中使用 StreamWriter。阅读效果很好,写作效果不同。我所看到的是,当我选择要写入的文件时,如果我选择
我正在尝试将现有文件保存到另一个地方。这是某种副本,但我想允许使用 FileSavePicker 的用户选择新的目的地。这是我的代码: StorageFile currentImage = await
我想编写一个 Win8 应用程序,我在那里使用了一个带有 static 成员的 Configuration 类,它在启动时加载一次,并且可以在任何地方访问运行时。 所以,主要问题是,默认设置存储在一个
以下代码将无法编译,因为 IsOfType 不被接受为项目的方法。文档指出: When this method completes successfully, it returns an IStora
我用下面的代码下载/保存了一张图片,稍后打开,但是在后面的OpenAsync中,抛出了UnauthorizedAccessException,看起来是文件没有关闭,实际上IRandomAccessSt
根据 http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.datecreated.aspx
我正在构建一个 Metro 应用程序,并尝试在将图像保存到 StorageFile 后获取图像的 Uri,这是我的代码: StorageFile file = await ApplicationDat
我正在检查我的数据文件夹中的所有文件,但没有一个被删除。我不确定,但我认为我做错了什么,但我不知道它是什么: private async void pageRoot_Unloaded(object s
我有一个 Metro 风格的应用程序,它每隔 X 秒轮询一次存储在独立存储中的文件(这是对 StorageFile 对象的引用)。 它是一个 PDF 文件,在我的应用程序中,我允许用户打开文档并使用
我想从一个路径手动创建一个StorageFile,但是没有StorageFile 的构造函数。 有没有简单的方法来做这样的事情? StorageFile f = new StorageFile("C:
我有一段代码可以在 URI 有效时从 http URI 正确加载图像,但我不知道如何捕获 OpenAsync 在 URI 无效时抛出的异常(结果为 404)。 问题是当包含调用 OpenAsync 的
我是一名优秀的程序员,十分优秀!