本地保存图像&显示windows phone 7

时间:2013-05-17 11:59:05

标签: windows-phone-7 local-storage

我正在尝试使用WP7实现一个新闻应用程序(feedrss)我已经尝试过这个解决方案,但它对我不起作用,这是我的代码:

 namespace PhoneApp1

 {
public partial class MainPage : PhoneApplicationPage
{
 public MainPage()
    {
        InitializeComponent();}
 private void Go_Click(object sender, RoutedEventArgs e)
    {

        WebClient _client = new WebClient();
        _client.DownloadStringCompleted += Feed;
        Location.Text = "http://www.aufaitmaroc.com/feeds/ma-vie.xml";
        _client.DownloadStringAsync(new Uri((Location.Text)));
        InitializeComponent();
    }
 private void Feed(object Sender, DownloadStringCompletedEventArgs e)
    {
        XElement _xml;
        try
        {
            if (!e.Cancelled)
            {
                _xml = XElement.Parse(e.Result);
                List<FeedItem> l = new List<FeedItem>();

                foreach (XElement value in _xml.Elements("channel").Elements("item"))
                {
                    FeedItem _item = new FeedItem();
                    _item.Title = value.Element("title").Value;

                    _item.enclosure = value.Element("enclosure").Attribute("url").Value;


                    _item.Description = Regex.Replace(value.Element("description").Value,
                    @"<(.|\n)*?>", String.Empty);
                    _item.Link = value.Element("link").Value;
                    _item.Guid = value.Element("guid").Value;
                    _item.Published = DateTime.Parse(value.Element("pubDate").Value);
                    l.Add(_item);



                    HttpWebRequest reqest1 = (HttpWebRequest)WebRequest.Create(_item.enclosure);
                    reqest1.BeginGetResponse(DownloadImageCallback, reqest1);
                    Thread.Sleep(1000);
                    WebClient client = new WebClient();
                    client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
                    client.OpenReadAsync(new Uri(_item.enclosure), client);

                }

                listBox.ItemsSource = l;

            }

        }
             catch
        {

        }
    }


    IsolatedStorageFile MyStore = IsolatedStorageFile.GetUserStoreForApplication();


    void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        var resInfo = new StreamResourceInfo(e.Result, null);
        var reader = new StreamReader(resInfo.Stream);
        byte[] contents;
        using (BinaryReader bReader = new BinaryReader(reader.BaseStream))
        {
            contents = bReader.ReadBytes((int)reader.BaseStream.Length);
        }
        IsolatedStorageFileStream stream = MyStore.CreateFile("10.jpg");
        stream.Write(contents, 0, contents.Length);
        stream.Close();
    }


    void DownloadImageCallback(IAsyncResult result)
    {

        HttpWebRequest req1 = (HttpWebRequest)result.AsyncState;
        HttpWebResponse responce = (HttpWebResponse)req1.EndGetResponse(result);
        Stream s = responce.GetResponseStream();
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            string directory = "Imagestest";
            if (!MyStore.DirectoryExists(directory))
            {
                MyStore.CreateDirectory(directory);
                 IsolatedStorageFileStream stream = MyStore.CreateFile("10.jpg");
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var isoFileStream = myIsolatedStorage.CreateFile(directory + "//ANALYSE_10052013095620.jpg"))
                    {

                       var bitimage = new BitmapImage();


                        var wb = new WriteableBitmap(bitimage);

                        var width = wb.PixelWidth;
                        var height = wb.PixelHeight;
                      //  bitimage.SetSource = isoFileStream;
                        image1.Source = bitimage;

                       System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
                    }
                }
            }
            else
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (myIsolatedStorage.FileExists(directory + "//ANALYSE_10052013095620.jpg"))
                    {
                        myIsolatedStorage.DeleteFile(directory + "//ANALYSE_10052013095620.jpg");
                    }

                    using (var isoFileStream = myIsolatedStorage.CreateFile(directory + "//ANALYSE_10052013095620.jpg"))
                    {

                        var bitimage = new BitmapImage();

                        var wb = new WriteableBitmap(bitimage);
                        var width = wb.PixelWidth;
                        var height = wb.PixelHeight;

                        System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
                    }
                }
            }
        });
    }
   private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        FeedItem currentFeedItem = (FeedItem)listBox.SelectedItem;
        PhoneApplicationService.Current.State["FeedItem"] = currentFeedItem;

     }

当我运行这个项目时,没有任何事情发生,它没有创建任何文件夹,当我停止互联网时,图像不再显示。

2 个答案:

答案 0 :(得分:1)

如果您的应用需要图像缓存,我可以为您推荐JetImageLoader

特点:

  • 在内存中缓存(因此它可以非常快速地使用列表,网格等)
  • 缓存是IsolatedStorageFile(以防止每次重新加载)
  • 完全异步(无滞后)
  • 通过XAML绑定转换器使用,您无需更改代码,只需为图像声明转换器

但它仅支持Windows Phone 8 +

我是该图书馆的作者,所以如果您有任何疑问,请在此写信给我或在github上创建一个问题

答案 1 :(得分:0)

如果要在本地保存图像并从本地保存显示图像,请尝试此示例 http://code.msdn.microsoft.com/wpapps/CSWP8ImageFromIsolatedStora-8dcf8411,在WP 8上,它运行正常。