2012-07-06 89 views
0

我是WP7应用程序开发的新手,我无法将参数传递到网站上的API。WebClient事件触发命令

我的理解是,当WP7打开一个页面时,onNavigatedTo()会首先被触发,但是当我尝试抓取参数时,首先会触发webClient_DownloadStringCompleted()。

public partial class Ranks : PhoneApplicationPage 
{ 
    private WebClient webClient; 
    private string pageType; 
    private string pagePosition; 
    public Ranks() 
    { 
     InitializeComponent(); 
     this.webClient = new WebClient(); 
     string header_auth = "application/json"; 
     this.webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted); 
     this.webClient.Headers[HttpRequestHeader.Authorization] = header_auth; 

     Uri serviceUri = new Uri(@"http://www.example.com/api/API.php?type=" + pageType + "&position=" + pagePosition); 
     this.webClient.DownloadStringAsync(serviceUri); 
    } 

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     string type, position; 
     if (NavigationContext.QueryString.TryGetValue("type", out type)) 
     { 
      pageType = type; 
     } 
     if (NavigationContext.QueryString.TryGetValue("pos", out position)) 
     { 
      pagePosition = position; 
     } 

    } 


    void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     string myJsonString = e.Result; 
     List<PlayerDetails> dataSource = new List<PlayerDetails>(); 

     //load into memory stream 
     using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(myJsonString))) 
     { 
      //parse into jsonser 
      var ser = new DataContractJsonSerializer(typeof(PlayerDetails[])); 
      PlayerDetails[] obj = (PlayerDetails[])ser.ReadObject(ms); 

      foreach (PlayerDetails plyr in obj) 
      { 

       dataSource.Add(plyr); 

      } 

      playerList.ItemsSource = dataSource; 

     } 


    } 

每当URI字符串建造它缺少的参数“的pagetype”和“pagePosition”

任何帮助将不胜感激!

回答

0

类构造函数将始终在OnNavigatedTo之前被调用。您应该将该代码从构造函数中移入OnNavigatedTo(或已加载)。

我猜你在构造函数中有这样的代码,因为你只希望它每页加载一次(即当用户导航返回页面时)。如果是这种情况,您可以检查NavigationMode

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     if (e.NavigationMode == NavigationMode.New) 
    { 
string type, position; 
     if (NavigationContext.QueryString.TryGetValue("type", out type)) 
     { 
      pageType = type; 
     } 
     if (NavigationContext.QueryString.TryGetValue("pos", out position)) 
     { 
      pagePosition = position; 
     } 

     this.webClient = new WebClient(); 
     string header_auth = "application/json"; 
     this.webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted); 
     this.webClient.Headers[HttpRequestHeader.Authorization] = header_auth; 

     Uri serviceUri = new Uri(@"http://www.example.com/api/API.php?type=" + pageType + "&position=" + pagePosition); 
     this.webClient.DownloadStringAsync(serviceUri); 

    } 




    } 
+0

这是非常有意义的......谢谢!我认为我遇到了模拟器缓存的问题,并且根据传递的参数不能正确加载页面。[br]我很欣赏这种回应。 – 2012-07-06 13:55:36