WP8从RSS链接下载

时间:2014-10-16 13:29:29

标签: c# windows-phone-8 visual-studio-2013

我正在尝试创建一个在RSS源上运行的下载应用。 我想要完成的是使用标题desc解析xml并且链接是列表框(我已经完成)但是当用户点击链接然后将JPG下载到手机时,我不能做任何帮助将不胜感激。

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    WebClient wc = new WebClient();
    wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
    wc.DownloadStringAsync(new Uri("Https://feed.xml"));
}

private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null)
        return;
    XElement xmlitems = XElement.Parse(e.Result);

   List<XElement> elements = xmlitems.Descendants("item").ToList();

   List<RSSItem> aux = new List<RSSItem>();
   foreach (XElement rssItem in elements)
   {
       RSSItem rss = new RSSItem();                
       rss.Description1 = rssItem.Element("description").Value;
       rss.Link1 = rssItem.Element("link").Value;
       rss.Title1 = rssItem.Element("title").Value;
       aux.Add(rss);
       TextBlock tbTitle = new TextBlock();
       tbTitle.Text = rss.Title1 + "\n";
       ListBoxRss.Items.Add(tbTitle);
       TextBlock tbDescription = new TextBlock();
       tbDescription.Text = rss.Description1 + "\n";
       ListBoxRss.Items.Add(tbDescription);
       TextBlock tbLink = new TextBlock();
       tbLink.Text = rss.Link1 + "\n";
       ListBoxRss.Items.Add(tbLink);

   }
}

你给我的信息工作得很好,但我有最后一个问题,应用程序加载精细列表xml罚款,当用户点击下载链接它只工作一次,一旦下载,如果用户点击另一个只有它记住第一次点击的标题和链接,直到我重新启动应用程序,这是我当前的代码

    public string fileurl;
    public string filetitle;
    public string filesave;

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
        wc.DownloadStringAsync(new Uri("https://feed.xml"));
    }

    private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
            return;
        XElement xmlitems = XElement.Parse(e.Result);
        List<XElement> elements = xmlitems.Descendants("item").ToList();   
        List<RSSItem> aux = new List<RSSItem>();
        foreach (XElement rssItem in elements)
        {
            RSSItem rss = new RSSItem();               
            rss.Description1 = rssItem.Element("description").Value;
            rss.Link1 = rssItem.Element("link").Value;
            rss.Title1 = rssItem.Element("title").Value;
            aux.Add(rss);
            TextBlock tbTitle = new TextBlock();
            tbTitle.Text = rss.Title1 + "\n";     
            ListBoxRss.Items.Add(tbTitle);
            TextBlock tbLink = new TextBlock();
            tbLink.Text = "Download: " + rss.Link1; 
            tbLink.MouseLeftButtonDown += new MouseButtonEventHandler(tbLink_MouseLeftButtonDown);
            ListBoxRss.Items.Add(tbLink);
            fileurl = rss.Link1;
            filetitle = rss.Description1;              
        }
    }

    private void tbLink_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {       
       WebClient client = new WebClient();
        client.OpenReadCompleted += client_OpenReadCompleted;
       client.OpenReadAsync(new Uri(fileurl));         
    }

    async void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        filesave = (filetitle + ".zip");
        byte[] buffer = new byte[e.Result.Length];
        await e.Result.ReadAsync(buffer, 0, buffer.Length);
        using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream stream = storageFile.OpenFile(filesave, FileMode.Create))
            {
                await stream.WriteAsync(buffer, 0, buffer.Length);
            }
        }

        try
        {               
            StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile pdffile = await local.GetFileAsync(filesave);
            Windows.System.Launcher.LaunchFileAsync(pdffile);
        }
        catch (Exception)
        {
            MessageBox.Show("File Not Found");
        }
    }

1 个答案:

答案 0 :(得分:1)

tbLink上注册'点击'事件,然后将其添加到ListBox。

...
    TextBlock tbLink = new TextBlock();
    tbLink.Text = rss.Link1 + "\n";

    //add the event handler
    tbLink.MouseLeftButtonDown += new MouseButtonEventHandler(tbLink_MouseLeftButtonDown);
    ListBoxRss.Items.Add(tbLink);
}

当用户点击TextBlock时,使用WebClient.OpenReadAsync(Uri)将在线图像读入字节数组(byte []),然后将字节数组保存到IsolatedStorage

private void tbLink_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    WebClient client = new WebClient();
    client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
    client.OpenReadAsync(new Uri((sender as TextBlock).Text));
}

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 = file.CreateFile("file.jpg");
    stream.Write(contents, 0, contents.Length);
    stream.Close();
}

代码示例is taken from here

根据您的评论进行更新:

我将链接信息(网址和说明)附加到tbLink,单击TextBlock时我可以获得 - 您可以看到差异,每个tbLink都有其唯一的URL和描述。

public string fileurl;
public string filetitle;
public string filesave;

private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    ...
    foreach (XElement rssItem in elements)
    {
        ...
        TextBlock tbLink = new TextBlock();
        tbLink.Text = "Download: " + rss.Link1; 

        //Add the link info to tbLink, you can get the info when tbLink is Clicked
        tbLink.Tag = new string[] {rss.Link1, rss.Description1};
        tbLink.MouseLeftButtonDown += new MouseButtonEventHandler(tbLink_MouseLeftButtonDown);
        ListBoxRss.Items.Add(tbLink);
    }
}

private void tbLink_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{       
   //get the link info from tbLink's Tag property
   string[] linkInfo = (sender as TextBlock).Tag as string[];
   fileurl = linkInfo[0];

   WebClient client = new WebClient();
   client.OpenReadCompleted += client_OpenReadCompleted;
   //pass the link info to the OpenReadCompleted callback
   client.OpenReadAsync(new Uri(fileurl), linkInfo);         
}

async void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    //and you get the link info from the e.UserState property
    string[] linkInfo = e.UserState as string[];
    filetitle = linkInfo[1];
    filesave = (filetitle + ".zip");
    ...
}