我有一个绑定到列表框的XML文件,如下所示。这将显示包含以下每个元素的20多个拍卖的列表。
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,
};
我希望用户能够点击其中一个拍卖,并将其从拍卖中传递给另一个页面,我将使用该列表例如.. 40008598来显示该拍卖的具体细节,如下所示:
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,
};
更新了代码并进行了更改########################################### / p>
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));
}
}
}
}
第二页查看列表详情########################################## ##
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; }
public 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)
{
}
}
}
答案 0 :(得分:1)
将SelectionChanged事件处理程序添加到listBox1
。在此处理程序中执行以下操作
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));
}
}
在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
}
}
请注意,您只需将列表ID转换为字符串,然后在导航时将其附加到Uri,因为ID是数字,因此它不会包含在Uri中被视为无效的任何字符。但是,如果要将其他字符串附加到Uri查询字符串,则应执行以下操作:
NavigationService.Navigate(
new Uri( String.Format( "/NewPage.xaml?queryString1={0}&queryString2={1}",
Uri.EscapeDataString( item.param1 ),
Uri.EscapeDataString( item.param2 ) ), UriKind.Relative ) );
上面的示例显示了如何将多个查询字符串传递给另一个页面。