gpt4 book ai didi

c# - 无法将 IStorageItem 转换为 StorageFile

转载 作者:可可西里 更新时间:2023-11-01 14:14:28 26 4
gpt4 key购买 nike

以下代码将无法编译,因为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com