1

编写异步加载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;
    }
}

方法 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;
        });
}

编译器发出以下错误:

  • 类或接口方法中的无效标记“void”
  • Stream imageStream = await LoadImageAsync(tempPhotoNote.Id); 内必填“;” 后await LoadImageAsync
4

0 回答 0