c# - Windows Phone - 图像缓存 - 等待下载图像

标签 c# windows-phone-7 caching

我做了一个缓存图像的项目。我想在主线程中等待完成 DownloadImage 函数,然后返回保存的位图。那可能吗? 我做得对吗?

public static ImageSource GetImage(int id)
    {
        BitmapImage bitmap = new BitmapImage();
        String fileName=string.Format("ImageCache/{0}.jpg", id);

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (!myIsolatedStorage.DirectoryExists("ImageCache"))
            {
                myIsolatedStorage.CreateDirectory("ImageCache");
            }

            if (myIsolatedStorage.FileExists(fileName))
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                {
                    bitmap.SetSource(fileStream);
                }
            }
            else
            {
                DownloadImage(id);
                //HERE - how to wait for end of DownloadImage and then do that below??
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                {
                    bitmap.SetSource(fileStream);
                }
            }
        }
        return bitmap;
    }

这是 DownloadImage 函数:

    private static void DownloadImage(Object id)
    {
        WebClient client = new WebClient();
        client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
        client.OpenReadAsync(new Uri(string.Format("http://example.com/{0}.jpg", id)), id);
    }
    private static void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (e.Error == null && !e.Cancelled)
            {
                try
                {
                    string fileName = string.Format("ImageCache/{0}.jpg", e.UserState);
                    IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(fileName);

                    BitmapImage image = new BitmapImage();
                    image.SetSource(e.Result);
                    WriteableBitmap wb = new WriteableBitmap(image);

                    // Encode WriteableBitmap object to a JPEG stream.
                    Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                    fileStream.Close();

                }
                catch (Exception ex)
                {
                    //Exception handle appropriately for your app  
                }
            }  
        }

    }

最佳答案

你有很多方法可以实现你想要的。可以使用 async await 命令等待,它是 Visual Studio Async 的一部分。 您可以从here下载最新的CTP。 . More how to use it.

我个人会使用事件。

关于c# - Windows Phone - 图像缓存 - 等待下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12768680/

相关文章:

php - PHP 中的引用计数

c# - 在 C# 中重载运算符 =。我怎样才能接受其他类型?

c# - 使用 linq 反转 ObservableCollection<objectType>

silverlight - Dispatcher.BeginInvoke lambda 捕获线程安全?

c# - 大型 asp.net 缓存存储持久化方法的效率

mysql - 我怎么知道什么时候该切换到执行从 MySQL 查询到 Redis 的读取操作?

c# - 从非泛型类的泛型方法派生泛型方法

c# - ReadAsync 从缓冲区获取数据

c# - 如何为 WMI 查询设置超时?

windows-phone-7 - 在NavigationService中清除后退堆栈