我有一个在异步方法上调用 await 的方法。
private async void LoadPic(string imageFileName)
{
StorageFolder sf = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder subFolder = await sf.GetFolderAsync("Assets");
StorageFile sfile = await subFolder.GetFileAsync(imageFileName);
当我在下一行设置断点时,断点永远不会被击中。它只是退出方法并返回。
public PixelData GrabPixelData(string imageFileName)
{
if (!ImageDictionary.ContainsKey(imageFileName))
{
// doesn't exist yet, so load it
LoadPic(imageFileName);
}
PixelData pd = new PixelData();
bool found = false;
while( found == false ) {
found = ImageDictionary.TryGetValue(imageFileName, out pd);
}
return pd;
// throw new NullReferenceException();
}
在我调用 LoadPic() 之后,我添加了一个循环,不断检查它是否将此文件中的图像添加到字典中。它似乎永远不会被添加,只是挂起。
这种方法在我从中抽象出来的子类上运行良好。
编辑:
修改了一些东西。现在一切似乎都正常了,但是当我将结果分配给子类时,我得到一个空异常错误(即使调试器没有指示任何内容为空!)。
我想知道它是否与它被包装在任务中的事实有关。
儿童班:
private async void LoadPic()
{
// I realize the async void is to be avoided, but ... I'm not sure because there isn't anything I want it to return. Should it just return a dummy task?
sat1.pixelData = await rootPage.GrabPixelData("sat1.png");
加载图片:
private async Task<PixelData> LoadPic(string imageFileName)
{
StorageFolder sf = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder subFolder = await sf.GetFolderAsync("Assets");
StorageFile sfile = await subFolder.GetFileAsync(imageFileName);
...
return pd;
抓取像素数据:
public async Task<PixelData> GrabPixelData(string imageFileName)
{
if (!ImageDictionary.ContainsKey(imageFileName))
{
// doesn't exist yet, so load it
PixelData pd = await LoadPic(imageFileName);
ImageDictionary.Add(imageFileName, pd);
}
var test = ImageDictionary[imageFileName];
return ImageDictionary[imageFileName];
}