我正在尝试从文件中读取数据,并且 GetFileFromApplicationUriAsync,大多数时候只是永远挂起,任何想法为什么会发生这种情况。我正在模拟器上调试应用程序。
几点注意事项:
BaseLocalFolder = "ms-appdata:///local/";
Uri baseUri = new Uri(BaseLocalFolder);
Uri cardsUri = new Uri(baseUri, Path.Combine(set,"cards.ccc"));
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(cardsUri);
using (IInputStream stream = await file.OpenSequentialReadAsync())
{
using (StreamReader rdr = new StreamReader(stream.AsStreamForRead()))
{
var cardArray = Windows.Data.Json.JsonArray.Parse(rdr.ReadToEnd());
foreach (Tables.Card card in cardArray.ToCards())
{
_cards.Add(card.ToCardViewModel());
}
}
}
更新:根据建议更改代码后,我开始遇到与 ReadTextAsync() 相同的问题,一旦它到达该行,它就会永远留在那里,我永远不会得到字符串。
StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(Path.Combine(set, "cards.ccc"));
try
{
string allCards = await Windows.Storage.FileIO.ReadTextAsync(file);
var cardArray = Windows.Data.Json.JsonArray.Parse(allCards);
foreach (Tables.Card card in cardArray.ToCards())
{
_cards.Add(card.ToCardViewModel());
}
}
catch (FileNotFoundException err)
{
string s = "";
}