web-dev-qa-db-ja.com

WebClient.DownloadStringAsync URLで文字列を使用する方法

私は現在、これを自分の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();
    }
}

そのWbeClient 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;
        }
        //Disaply Specfic 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



    }
}
15
Rhys

それらは同じページにありますか?その場合は、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を設定するのがおそらく最善です。これにより、毎回新しいインスタンスを作成する必要がなくなります。

8
keyboardP

これはあなたがする必要があることです。

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
}

そこにあなたのテキストがあります。

12
Ignacio Barreto

タスクを使用する別の方法:

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