Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从RSS链接下载WP8_C#_Windows Phone 8_Visual Studio 2013 - Fatal编程技术网

C# 从RSS链接下载WP8

C# 从RSS链接下载WP8,c#,windows-phone-8,visual-studio-2013,C#,Windows Phone 8,Visual Studio 2013,我正在尝试创建一个在RSS提要上运行的下载应用程序。 我试图完成的是一个get解析xml,它带有一个标题desc和一个链接,是一个列表框(我已经完成了),但是当用户点击链接,然后下载一个JPG到手机上时,我不能做任何帮助,我将不胜感激 private void MainPage_Loaded(object sender, RoutedEventArgs e) { WebClient wc = new WebClient(); wc.DownloadStringCompleted

我正在尝试创建一个在RSS提要上运行的下载应用程序。 我试图完成的是一个get解析xml,它带有一个标题desc和一个链接,是一个列表框(我已经完成了),但是当用户点击链接,然后下载一个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);

   }
}
private void主页\u已加载(对象发送方、路由目标方)
{
WebClient wc=新的WebClient();
wc.DownloadStringCompleted+=新的DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
DownloadStringAsync(新Uri(“Https://feed.xml"));
}
私有void wc_DownloadStringCompleted(对象发送方,DownloadStringCompletedEventArgs e)
{
如果(例如错误!=null)
返回;
XElement xmlitems=XElement.Parse(e.Result);
List elements=xmlitems.subjects(“item”).ToList();
List aux=新列表();
foreach(元素中的元素rssItem)
{
RSSItem rss=新的RSSItem();
rss.Description1=rssItem.Element(“description”).Value;
rss.Link1=rssItem.Element(“link”).Value;
rss.Title1=rssItem.Element(“title”).Value;
辅助添加(rss);
TextBlock tbTitle=新的TextBlock();
tbTitle.Text=rss.Title1+“\n”;
ListBoxRss.Items.Add(tbTitle);
TextBlock tbDescription=新建TextBlock();
tbDescription.Text=rss.Description1+“\n”;
ListBoxRss.Items.Add(tbDescription);
TextBlock tbLink=新的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");
        }
    }
公共字符串fileurl;
公共字符串文件名;
公共字符串文件保存;
已加载私有void主页(对象发送方、路由目标)
{
WebClient wc=新的WebClient();
wc.DownloadStringCompleted+=新的DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(新Uri(“https://feed.xml"));
}
私有void wc_DownloadStringCompleted(对象发送方,DownloadStringCompletedEventArgs e)
{
如果(例如错误!=null)
返回;
XElement xmlitems=XElement.Parse(e.Result);
List elements=xmlitems.subjects(“item”).ToList();
List aux=新列表();
foreach(元素中的元素rssItem)
{
RSSItem rss=新的RSSItem();
rss.Description1=rssItem.Element(“description”).Value;
rss.Link1=rssItem.Element(“link”).Value;
rss.Title1=rssItem.Element(“title”).Value;
辅助添加(rss);
TextBlock tbTitle=新的TextBlock();
tbTitle.Text=rss.Title1+“\n”;
ListBoxRss.Items.Add(tbTitle);
TextBlock tbLink=新的TextBlock();
tbLink.Text=“下载:”+rss.Link1;
tbLink.MouseLeftButtonDown+=新的MouseButtonEventHandler(tbLink\u MouseLeftButtonDown);
ListBoxRss.Items.Add(tbLink);
fileurl=rss.Link1;
filetitle=rss.Description1;
}
}
私有void tbLink_MouseLeftButtonDown(对象发送器,MouseButtonEventArgs e)
{       
WebClient客户端=新的WebClient();
client.OpenReadCompleted+=客户机_OpenReadCompleted;
OpenReadAsync(新Uri(fileurl));
}
异步无效客户端\u OpenReadCompleted(对象发送方,OpenReadCompletedEventArgs e)
{
filesave=(filetitle+“.zip”);
字节[]缓冲区=新字节[e.Result.Length];
等待e.Result.ReadAsync(buffer,0,buffer.Length);
使用(IsolatedStorageFile storageFile=IsolatedStorageFile.GetUserStoreForApplication())
{
使用(IsolatedStorageFileStream=storageFile.OpenFile(filesave,FileMode.Create))
{
wait stream.WriteAsync(buffer,0,buffer.Length);
}
}
尝试
{               
StorageFolder local=Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile pdffile=await local.GetFileAsync(filesave);
Windows.System.Launcher.LaunchFileAsync(pdffile);
}
捕获(例外)
{
MessageBox.Show(“未找到文件”);
}
}

在将其添加到列表框之前,在
tbLink
上注册“单击”事件

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

    //add the event handler
    tbLink.MouseLeftButtonDown += new MouseButtonEventHandler(tbLink_MouseLeftButtonDown);
    ListBoxRss.Items.Add(tbLink);
}
当用户点击文本块时,您可以使用
WebClient.OpenReadAsync(Uri)
将在线图像读取到字节数组(字节[]),然后将字节数组保存到
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();
}
代码示例

根据您的评论更新:

我将链接信息(url和描述)附加到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");
    ...
}

嗨,谢谢你的提示,效果很好我现在有一个非常奇怪的问题我编辑了我的帖子谢谢你是个天才!,是否有任何方法可以保存到下载文件夹,因为出于某种原因,如果我下载了一个zip文件,它不会与任何zip应用程序一起工作,因此我想在计算机上测试新下载的文件,以确保其有效性。幸运的是,您无法通过编写代码从应用程序访问下载文件夹-出于安全原因,它受到限制。而且你的文件根本没有压缩,你只需将其文件扩展名从.png更改为.zip,当然它不会在zip应用程序中打开。尝试在WP8上编写zip文件。我已经这样做了,我使用了手动URL,而不是从提要和手动保存名称来确定,文件下载很好,但当我打开它们时,它们已损坏,它们可以在pc上下载和打开,但当我通过