using async await on Windows Phone 7

寵の児 提交于 2019-12-24 10:57:23

问题


Write the code which asynchronus load BitmapImage.

public async void AsyncLoadPhotoNotesFromIsolatedStorage()
{
    IsolatedStorageSettings appStorageSettings = IsolatedStorageSettings.ApplicationSettings;
    if (appStorageSettings.Count > 0)
    {
        var loadedPhotoNotes = new ObservableCollection<PhotoNote>();
        foreach (Object obj in appStorageSettings.Values)
        {
            var tempPhotoNote = (PhotoNote) obj;
            BitmapImage bitmapImage = null;
            Stream imageStream = await LoadImageAsync(tempPhotoNote.Id);
            if (imageStream != null)
            {   
                bitmapImage = new BitmapImage();
                bitmapImage.SetSource(imageStream);
            }

            tempPhotoNote.Picture = bitmapImage;
            loadedPhotoNotes.Insert(0, tempPhotoNote);
        }
        //loadedPhotoNotes.Reverse();
        PhotoNotes = loadedPhotoNotes;
    }
}

Method LoadImageAsync:

private Task<Stream> LoadImageAsync(string fileName)
{
    return Task.Factory.StartNew(() =>
        {
            if (fileName == null)
            {
                throw new ArgumentException("one of parameters is null");
            }

            Stream fileStream = null;

            using (var appStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (appStorageFile.FileExists(fileName))
                {
                    fileStream = appStorageFile.OpenFile(fileName, FileMode.Open, FileAccess.Read);
                }
            }
            return fileStream;
        });
}

The compiler issues the following error:

  • Invalid token 'void' inside the class or interface method
  • In line Stream imageStream = await LoadImageAsync(tempPhotoNote.Id); required ";" after await LoadImageAsync

来源:https://stackoverflow.com/questions/16748383/using-async-await-on-windows-phone-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!