- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
以下代码将无法编译,因为 IsOfType 不被接受为项目的方法。文档指出:
When this method completes successfully, it returns an IStorageItem that represents the specified file or folder. If the specified file or folder is not found, this method returns null instead of raising an exception.
To work with the returned item, call the IsOfType method of the IStorageItem interface to determine whether the item is a file or a folder. Then cast the item to a StorageFolder or StorageFile.
private async void RestoreData(string fileName)
{
StorageFolder folder = ApplicationData.Current.LocalFolder;
var item = folder.TryGetItemAsync(fileName);
if (item == null)
{
existingData = false;
}
if (item.IsOfType(StorageItemTypes.File))
{
await ReadDataAsync(item as StorageFile);
existingData = true;
}
existingData = false;
}
最佳答案
你错过了await
等待异步方法结果的关键字 - 在这种情况下,您应该这样写:
var item = await folder.TryGetItemAsync(fileName);
多亏了item
类型为 IStorageItem
而在您的代码中,它的类型是 IAsyncOperation<IStorageItem>
.
通过 convention , 所有异步方法都有 Async
后缀,你需要等待结果来检查它。
提示:
在大多数 IDE 中,当您将鼠标悬停在 var
上时关键字,将显示一种类型的变量 - 这使得查找错误变得更加容易。
关于c# - 无法将 IStorageItem 转换为 StorageFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37425818/
以下代码将无法编译,因为 IsOfType 不被接受为项目的方法。文档指出: When this method completes successfully, it returns an IStora
我正在开发一个能够共享其文件的 UWP 应用程序。我关注了the documentation from Microsoft该解决方案运行良好。 这是我的实现: public void ShareLoc
我是一名优秀的程序员,十分优秀!