Web图像路径到本地路径值转换器

时间:2014-10-28 16:59:49

标签: c# windows-phone-8 windows-8

我想从网上加载图片,但要缓存它们,然后从本地存储文件夹中显示给用户。

我有以下IValueConverter类:

public class ImageToIsolatedPathConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var fileName = (string)value;

        if (NetworkInterface.GetIsNetworkAvailable())
        {
            LoadImage(fileName);
        }

        try
        {
            var file = Path.GetFileName(fileName);
            // TODO code to retrieve file and return it
        }
        catch (Exception ex)
        {
        }

        return fileName;
    }
}

LoadImage(..)如下:

private void LoadImage(string imagePath)
{
    var imageFileName = Path.GetFileName(imagePath);
    var webClient = new WebClient();
    webClient.OpenReadCompleted += async (s1, e1) =>
    {
        if (e1.Error == null)
        {
            try
            {
                bool isSpaceAvailable = IsSpaceIsAvailable(e1.Result.Length);

                if (isSpaceAvailable)
                {
                    // TODO save file using ApplicationData.Current.LocalFolder

                    using (var isfs = new IsolatedStorageFileStream(imageFileName,
                                        FileMode.OpenOrCreate,
                                        IsolatedStorageFile.GetUserStoreForApplication()))
                    {
                        long fileLen = e1.Result.Length;
                        byte[] b = new byte[fileLen];
                        e1.Result.Read(b, 0, b.Length);
                        isfs.Write(b, 0, b.Length);
                        isfs.Flush();
                    }
                }
                else
                {
                    // not enough space
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        else
        {
            MessageBox.Show(e1.Error.Message);
        }
    };

    webClient.OpenReadAsync(new Uri(imagePath));
}

如何使用ApplicationData.Current.LocalFolder中的WebClient保存文件,然后从同一StorageFolder读取文件?

我需要为Windows Phone 8和Windows 8应用程序执行此操作。

有什么想法吗?

0 个答案:

没有答案