当前位置: 首页 > 编程语言 > C#

如何在WebClient.DownloadStringAsyncURL中使用字符串共享

时间:2023-04-10 10:41:24 C#

如何在WebClient.DownloadStringAsyncURL中使用字符串我目前的WebClientURL有这个DownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);Detail.DownloadStringAsync(newUri("http://api.trademe.co.nz/v1/Listings/"+ListingID.Text+".xml"));我想要做的是使用这个字符串:if(lbi!=null){字符串id=lbi.ListingId.ToString();}}作为WbeClientURL的一部分。例如:WebClientDetail=newWebClient();Detail.DownloadStringCompleted+=newDownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);Detail.DownloadStringAsync(newUri("http://api.trademe.co.nz/v1/Listings/"+id+".xml"));无论如何在URL中使用这个字符串如上所示?完整代码#####################################使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Net;使用System.Windows;使用System.Windows.Controls;使用System.Windows.Documents;使用System.Windows.Input;使用System.Windows.Media;使用System.Windows.Media.Animation;使用System.Windows.Shapes;使用Microsoft.Phone.Controls;使用System.Xml.Linq;namespaceTradeMe_Panorama{publicpartialclassMainPage:PhoneApplicationPage{//构造函数publicMainPage(){InitializeComponent();//将列表框控件的数据上下文设置为示例数据DataContext=App.ViewModel;this.Loaded+=newRoutedEventHandler(MainPage_Loaded);}//加载data用于ViewModel项目privatevoidMainPage_Loaded(objectsender,RoutedEventArgse){if(!App.ViewModel.IsDataLoaded){}}privatevoidbutton1_Click(objectsender,RoutedEventArgse){WebClientTrademe=newWebClient();Trademe.DownloadStringCompleted+=newDownloadStringCompletedEventHandler(Trademe_DownloadStringCompleted);Trademe.DownloadStringAsync(newUri("http://api.trademe.co.nz/v1/Search/General.xml?search_string="+TradeSearch.Text));progressBar1.IsIndeterminate=true;progressBar1.Visibility=Visibility.Visible;}//显示已使用的一般产品的列表####################################################################voidTrademe_DownloadStringCompleted(对象发送者,Dow??nloadStringCompletedEventArgse){如果(e.Error!=null)返回;varr=XDocument.Parse(e.Result);//声明命名空间XNamespacens="http://api.trademe.co.nz/v1";TradeSearch1.ItemsSource=来自r.Root.Desc中的TMendants(ns+"Listing").Take(20)选择新的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;}//显示二手车清单##################################################################privatevoidbutton2_Click(objectsender,RoutedEventArgse){WebClientMotor=newWebClient();Motor.DownloadStringCompleted+=newDownloadStringCompletedEventHandler(Motor_DownloadStringCompleted);Motor.DownloadStringAsync(newUri("http://api.trademe.co.nz/v1/Search/Motors/Used.xml?search_string="+MotorSearch.Text));progressBar1.IsIndeterminate=true;progressBar1.Visibility=Visibility.Visible;}voidMotor_DownloadStringCompleted(对象发送者,Dow??nloadStringCompletedEventArgse){if(e.Error!=null)返回;varr=XDocument.Parse(e.Result);//声明命名空间XNamespacens="http://api.trademe.co.nz/v1";MotorsListings.ItemsSource=fromMinr.Root.Descendants(ns+"Car").Take(20)选择新的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")。价值,};progressBar1.IsIndeterminate=false;progressBar1.Visibility=Visibility.Collapsed;}//显示列表的具体细节########################################################################privatevoidbutton4_Click(objectsender,RoutedEventArgse){WebClientDetail=newWebClient();Detail.DownloadStringCompleted+=newDownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);Detail.DownloadStringAsync(newUri("http://api.trademe.co.nz/v1/Listings/"+ListingID.Text+".xml"));progressBar1.IsIndeterminate=true;progressBar1.Visibility=Visibility.Visible;}voidDetail_DownloadStringCompleted(objectsender,DownloadStringCompletedEventArgse){if(e.Error!=null)return;varr=XDocument.Parse(e.Result);//声明命名空间XNamespacens="http://api.trademe.co.nz/v1";ListingDetails.ItemsSource=fromDinr.Descendants(ns+"ListedItemDetail").Take(20)选择新的贸易项目{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=Visi能力。崩溃;}publicclassTradeItem{publicstringRegion{get;放;}publicstringListingId{得到;放;}publicstringPriceDisplay{get;放;}公共字符串标题{得到;放;}publicstringImageSource{得到;放;}publicstringCloseDate{get;放;}publicstringStartPrice{get;放;}publicstringBuyNow{get;放;}}//运行查询字符串以将LIstingID传递到下一页}}它们在同一页上?如果是这样,那么您可以使id变量成为类级别变量,以便可以从类中的任何位置访问它。或者,您可以将WebClient部分放在接受字符串的方法中。例如:privatevoidDownloadInformation(stringid){WebClientDetail=newWebClient();Detail.DownloadStringCompleted+=newDownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);Detail.DownloadStringAsync(newUri("http://api.trademe.co.nz/v1/Listings/"+id+".xml"));然后在那个ListboxSelectionChanged处理程序中,只需调用上面的方法。if(lbi!=null){字符串id=lbi.ListingId.ToString();下载信息(id);作为旁注,最好在方法之外设置WebClient,例如在Page的Loaded事件中。这样,您不必每次都创建一个新实例。这是您需要做的。WebClientwc=newWebClient();wc.DownloadStringCompleted+=newDownloadStringCompletedEventHandler(wc_DownloadStringCompleted);wc.DownloadStringAsync(newUri("http://api.trademe.co.nz/v1/Listings/"+id+".xml"));voidwc_DownloadStringCompleted(objectsender,DownloadStringCompletedEventArgse){stringtext=e.Result;//...对结果做一些事情}在那里你会得到你的文本。以上就是C#学习教程:HowtouseStringinWebClient.DownloadStringAsyncURL分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: