如何在 WebClient.DownloadStringAsync URL 中使用字符串

发布于 2024-11-30 16:00:45 字数 7412 浏览 0 评论 0原文

我目前的 WebClient URL 是这样的:

WebClient Detail = new WebClient();
Detail.DownloadStringCompleted += new
    DownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);
Detail.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Listings/" +
    ListingID.Text + ".xml"));

我想要做的是使用这个字符串:

void ListBoxSelectionChanged(object sender, SelectionChangedEventArgs args)
{
    var lbi = ((sender as ListBox).SelectedItem as TradeItem);
    if(lbi != null)
    {
        string id = lbi.ListingId.ToString();
    }
}

作为该 WebClient URL 的一部分。

示例:

WebClient Detail = new WebClient();
Detail.DownloadStringCompleted += new
    DownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);
Detail.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Listings/" + id +
    ".xml"));

是否可以在 URL 中使用此字符串,如上所示?

完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;

namespace TradeMe_Panorama
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor.
        public MainPage()
        {
            InitializeComponent();
            // Set the data context of the listbox control to the sample data.
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel items.
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            WebClient Trademe = new WebClient();
            Trademe.DownloadStringCompleted += new   
            DownloadStringCompletedEventHandler(Trademe_DownloadStringCompleted);
            Trademe.DownloadStringAsync(new
            Uri("http://api.trademe.co.nz/v1/Search/General.xml?search_string=" +
            TradeSearch.Text));
            progressBar1.IsIndeterminate = true;
            progressBar1.Visibility = Visibility.Visible;
        }

        // Display listing for used general products:
        void Trademe_DownloadStringCompleted(object sender,
            DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            var r = XDocument.Parse(e.Result);
            // Declare the namespace.
            XNamespace ns = "http://api.trademe.co.nz/v1";
            TradeSearch1.ItemsSource = from TM in r.Root.Descendants(ns +
                "Listing").Take(20)
            select new TradeItem
            {
                ImageSource = TM.Element(ns + "PictureHref").Value,
                Title = TM.Element(ns + "Title").Value,
                Region = TM.Element(ns + "Region").Value,
                PriceDisplay = TM.Element(ns + "PriceDisplay").Value,
                ListingId = TM.Element(ns + "ListingId").Value,
            };
            progressBar1.IsIndeterminate = false;
            progressBar1.Visibility = Visibility.Collapsed;
        }

        // Display listing for used Cars.
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            WebClient Motor = new WebClient();
            Motor.DownloadStringCompleted += new
            DownloadStringCompletedEventHandler(Motor_DownloadStringCompleted);
            Motor.DownloadStringAsync(new
            Uri("http://api.trademe.co.nz/v1/Search/Motors/Used.xml?search_string=" +
            MotorSearch.Text));
            progressBar1.IsIndeterminate = true;
            progressBar1.Visibility = Visibility.Visible;
        }        

        void Motor_DownloadStringCompleted(object sender,
            DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            var r = XDocument.Parse(e.Result);
            // Declare the namespace.
            XNamespace ns = "http://api.trademe.co.nz/v1";
            MotorsListings.ItemsSource = from M in r.Root.Descendants(ns +
                "Car").Take(20)
            select new TradeItem
            {
                ImageSource = M.Element(ns + "PictureHref").Value,
                Title = M.Element(ns + "Title").Value,
                Region = M.Element(ns + "Region").Value,
                PriceDisplay = M.Element(ns + "PriceDisplay").Value,
                ListingId = M.Element(ns + "ListingId").Value,
            };
            progressBar1.IsIndeterminate = false;
            progressBar1.Visibility = Visibility.Collapsed;
        }

        //Display specific details of listings:
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            WebClient Detail = new WebClient();
            Detail.DownloadStringCompleted += new
            DownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);
            Detail.DownloadStringAsync(new
            Uri("http://api.trademe.co.nz/v1/Listings/" + ListingID.Text +
            ".xml"));
            progressBar1.IsIndeterminate = true;
            progressBar1.Visibility = Visibility.Visible;
        }
        
        void Detail_DownloadStringCompleted(object sender,
            DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            var r = XDocument.Parse(e.Result);
            // Declare the namespace.
            XNamespace ns = "http://api.trademe.co.nz/v1";
            ListingDetails.ItemsSource = from D in r.Descendants(ns +
            "ListedItemDetail").Take(20)
            select new TradeItem
            {
                ImageSource = D.Element(ns + "Photos").Element(ns +
                "Photo").Element(ns + "Value").Element(ns + "Medium").Value,
                Title = D.Element(ns + "Title").Value,
                Region = D.Element(ns + "Region").Value,
                PriceDisplay = D.Element(ns + "Body").Value,
                ListingId = D.Element(ns + "ListingId").Value,
                CloseDate = D.Element(ns + "EndDate").Value,
                BuyNow = D.Element(ns + "BuyNowPrice").Value,
                StartPrice = D.Element(ns + "StartPrice").Value,
            };
            progressBar1.IsIndeterminate = false;
            progressBar1.Visibility = Visibility.Collapsed;
        }

        public class TradeItem
        {
            public string Region { get; set; }
            public string ListingId { get; set; }
            public string PriceDisplay { get; set; }
            public string Title { get; set; }
            public string ImageSource { get; set; }
            public string CloseDate { get; set;  }
            public string StartPrice { get; set; }
            public string BuyNow { get; set; }
        }
        // Run query string to pass LIstingID to next page.
    }
}

I currently have this for my WebClient URL:

WebClient Detail = new WebClient();
Detail.DownloadStringCompleted += new
    DownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);
Detail.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Listings/" +
    ListingID.Text + ".xml"));

What I want to do is use this string:

void ListBoxSelectionChanged(object sender, SelectionChangedEventArgs args)
{
    var lbi = ((sender as ListBox).SelectedItem as TradeItem);
    if(lbi != null)
    {
        string id = lbi.ListingId.ToString();
    }
}

As part of that WebClient URL.

Example:

WebClient Detail = new WebClient();
Detail.DownloadStringCompleted += new
    DownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);
Detail.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Listings/" + id +
    ".xml"));

Is there anyway to use this string in URL as show above?

Complete code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;

namespace TradeMe_Panorama
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor.
        public MainPage()
        {
            InitializeComponent();
            // Set the data context of the listbox control to the sample data.
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel items.
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            WebClient Trademe = new WebClient();
            Trademe.DownloadStringCompleted += new   
            DownloadStringCompletedEventHandler(Trademe_DownloadStringCompleted);
            Trademe.DownloadStringAsync(new
            Uri("http://api.trademe.co.nz/v1/Search/General.xml?search_string=" +
            TradeSearch.Text));
            progressBar1.IsIndeterminate = true;
            progressBar1.Visibility = Visibility.Visible;
        }

        // Display listing for used general products:
        void Trademe_DownloadStringCompleted(object sender,
            DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            var r = XDocument.Parse(e.Result);
            // Declare the namespace.
            XNamespace ns = "http://api.trademe.co.nz/v1";
            TradeSearch1.ItemsSource = from TM in r.Root.Descendants(ns +
                "Listing").Take(20)
            select new TradeItem
            {
                ImageSource = TM.Element(ns + "PictureHref").Value,
                Title = TM.Element(ns + "Title").Value,
                Region = TM.Element(ns + "Region").Value,
                PriceDisplay = TM.Element(ns + "PriceDisplay").Value,
                ListingId = TM.Element(ns + "ListingId").Value,
            };
            progressBar1.IsIndeterminate = false;
            progressBar1.Visibility = Visibility.Collapsed;
        }

        // Display listing for used Cars.
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            WebClient Motor = new WebClient();
            Motor.DownloadStringCompleted += new
            DownloadStringCompletedEventHandler(Motor_DownloadStringCompleted);
            Motor.DownloadStringAsync(new
            Uri("http://api.trademe.co.nz/v1/Search/Motors/Used.xml?search_string=" +
            MotorSearch.Text));
            progressBar1.IsIndeterminate = true;
            progressBar1.Visibility = Visibility.Visible;
        }        

        void Motor_DownloadStringCompleted(object sender,
            DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            var r = XDocument.Parse(e.Result);
            // Declare the namespace.
            XNamespace ns = "http://api.trademe.co.nz/v1";
            MotorsListings.ItemsSource = from M in r.Root.Descendants(ns +
                "Car").Take(20)
            select new TradeItem
            {
                ImageSource = M.Element(ns + "PictureHref").Value,
                Title = M.Element(ns + "Title").Value,
                Region = M.Element(ns + "Region").Value,
                PriceDisplay = M.Element(ns + "PriceDisplay").Value,
                ListingId = M.Element(ns + "ListingId").Value,
            };
            progressBar1.IsIndeterminate = false;
            progressBar1.Visibility = Visibility.Collapsed;
        }

        //Display specific details of listings:
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            WebClient Detail = new WebClient();
            Detail.DownloadStringCompleted += new
            DownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);
            Detail.DownloadStringAsync(new
            Uri("http://api.trademe.co.nz/v1/Listings/" + ListingID.Text +
            ".xml"));
            progressBar1.IsIndeterminate = true;
            progressBar1.Visibility = Visibility.Visible;
        }
        
        void Detail_DownloadStringCompleted(object sender,
            DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            var r = XDocument.Parse(e.Result);
            // Declare the namespace.
            XNamespace ns = "http://api.trademe.co.nz/v1";
            ListingDetails.ItemsSource = from D in r.Descendants(ns +
            "ListedItemDetail").Take(20)
            select new TradeItem
            {
                ImageSource = D.Element(ns + "Photos").Element(ns +
                "Photo").Element(ns + "Value").Element(ns + "Medium").Value,
                Title = D.Element(ns + "Title").Value,
                Region = D.Element(ns + "Region").Value,
                PriceDisplay = D.Element(ns + "Body").Value,
                ListingId = D.Element(ns + "ListingId").Value,
                CloseDate = D.Element(ns + "EndDate").Value,
                BuyNow = D.Element(ns + "BuyNowPrice").Value,
                StartPrice = D.Element(ns + "StartPrice").Value,
            };
            progressBar1.IsIndeterminate = false;
            progressBar1.Visibility = Visibility.Collapsed;
        }

        public class TradeItem
        {
            public string Region { get; set; }
            public string ListingId { get; set; }
            public string PriceDisplay { get; set; }
            public string Title { get; set; }
            public string ImageSource { get; set; }
            public string CloseDate { get; set;  }
            public string StartPrice { get; set; }
            public string BuyNow { get; set; }
        }
        // Run query string to pass LIstingID to next page.
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舂唻埖巳落 2024-12-07 16:00:45

使用任务的另一种方式:

using (WebClient client = new WebClient())
{                
    result = await client.DownloadStringTaskAsync(urlString);
}

another way it to use Task:

using (WebClient client = new WebClient())
{                
    result = await client.DownloadStringTaskAsync(urlString);
}
请你别敷衍 2024-12-07 16:00:45

这就是您需要做的:

WebClient wc = new WebClient();
wc.DownloadStringCompleted += new
    DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Listings/" +
    id + ".xml"));

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    string text = e.Result;
    // … do something with result.
}

在那里您将看到您的文本。

This is what you need to do:

WebClient wc = new WebClient();
wc.DownloadStringCompleted += new
    DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Listings/" +
    id + ".xml"));

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    string text = e.Result;
    // … do something with result.
}

There you will have your text.

小清晰的声音 2024-12-07 16:00:45

他们在同一页面上吗?如果是这样,那么您可以将 id 变量设置为类级变量,以便可以从类中的任何位置访问它。或者,您可以将 WebClient 部分放入接受字符串的方法中。例如:

private void DownloadInformation(string id)
{
    WebClient Detail = new WebClient();
    Detail.DownloadStringCompleted += new        DownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);
    Detail.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Listings/" + id + ".xml"));
}

然后在 ListboxSelectionChanged 处理程序中,只需调用上述方法即可。

if(lbi != null)
{
    string id = lbi.ListingId.ToString();
    DownloadInformation(id);
}

附带说明一下,最好在方法外部设置 WebClient,例如在页面的 Loaded 事件中。这样,就不必每次都创建一个新实例。

Are they on the same page? If so, then you can make the id variable a class-level variable so that it can be accessed from anywhere in the class. Alternatively, you could put the WebClient section into a method that accepts a string. For example:

private void DownloadInformation(string id)
{
    WebClient Detail = new WebClient();
    Detail.DownloadStringCompleted += new        DownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);
    Detail.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Listings/" + id + ".xml"));
}

Then in that ListboxSelectionChanged handler, simply call the above method.

if(lbi != null)
{
    string id = lbi.ListingId.ToString();
    DownloadInformation(id);
}

As a side note, it's probably best to have the WebClient set up outside the method, such as in the Page's Loaded event. This way, a new instance won't have to be created each time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文