开发者

How to pass a press XML result to another page

I have an XML file that is bound to a listbox as below. This displays a list of 20+ auctions all with each of the following elements.

listBox1.ItemsSource = from TM in r.Root.Descendants(ns + "Listing")
                                   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,
                                   };

I want the user to be able to click on one of the auctions and have that pass the Listingid from the auction to another page where I will use that Listingid eg.. 40008598 to display specfic details on that auction as per below:

WebClient Detail = new WebClient();
            Detail.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);
            Detail.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Listings/" + ***listingid from other previous page selection*** + ".xml"));

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,
                                         };

Updated Code with changes ##########################################

    First Page - View Auction Listings

   namespace TradeMe
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        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;


        }



        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";
            listBox1.ItemsSource = from TM in r.Root.Descendants(ns + "Listing")
                                   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;
        }



        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; }
        }

        private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/TestPage.xaml", UriKind.Relative));
        }

        private void eventhandler(object sender, SelectionChangedEventArgs e)

        {
            var item = (sender as ListBox).SelectedItem as TradeItem;

            if (item != null)
            {
                string id = TradeItem.ListingId.ToString();
                NavigationService.Navigate(new Uri("/TestPage.xaml?ListingId=" + id, UriKind.Relative));
            }
        }


        }


    }

Second Page View listing details ############################################

    namespace TradeMe
{
    public partial class TestPage : PhoneApplicationPage
    {
        public TestPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (NavigationContext.QueryString.ContainsKey("ListingId"))
            {
                var listingId = NavigationContext.QueryString["ListingId"];

                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=" + listingId + ".xml"));

                progressBar1.IsIndeterminate = true;
                progressBar1.Visibility = Visibility.Visible;
            }
        }

        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";
            ListingDetails.ItemsSource = from TM in r.Root.Descendants(ns + "Listing")
                                   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;
        }



        public class TradeItem
        {
            public string Region { get; set; }
            public string ListingId { get; set; }
            public string PriceDisplay { get; set; }
            pub开发者_运维知识库lic string Title { get; set; }
            public string ImageSource { get; set; }
        }

        private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {

        }


        }



    }


Add a SelectionChanged event handler to listBox1. In this handler do the following

void ListBoxSelectionChanged(object sender, SelectionChangedEventArgs args)
{
  var item = (sender as ListBox).SelectedItem as TradeItem;

  if(item != null) {
    string id = item.ListingId.ToString();
    NavigationService.Navigate(
      new Uri("/AuctionDetailsPage.xaml?ListingId=" + id, UriKind.Relative));    
  }
}

In AuctionDetailsPage.xaml.cs

protected override void OnNavigatedTo( NavigationEventArgs e )
{
  base.OnNavigatedTo( e );

  if( NavigationContext.QueryString.ContainsKey( "ListingId" ) ) {
    var listingId = NavigationContext.QueryString["ListingId"];

    // use WebClient to get the details
  }
}

Note that you can simply convert the listing ID to a string and append it to the Uri when navigating because the ID is numeric and so it will not contain any characters that will be considered invalid in a Uri. However, if you were appending some other string to the Uri query string you should do the following:

NavigationService.Navigate(
  new Uri( String.Format( "/NewPage.xaml?queryString1={0}&queryString2={1}",
    Uri.EscapeDataString( item.param1 ),
    Uri.EscapeDataString( item.param2 ) ), UriKind.Relative ) );

The above example shows how to pass multiple query strings to another page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜