0

我正在尝试通过使用 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;

     }

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

4

2 回答 2

1

如果您需要在应用程序中缓存图像,我可以JetImageLoader为您推荐库

特征:

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

但它只支持 Windows Phone 8+

我是那个图书馆的作者,所以如果你有任何问题,请在这里写信给我或在 github 上创建问题

于 2013-10-27T01:47:11.710 回答
0

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

于 2013-05-17T12:05:37.280 回答