2012-04-03 29 views
0

當我使用這個時,我必須重新啓動應用程序才能刷新數據。有幾個帖子說我應該在最後加上一個隨機數字,但是我不能這麼做,因爲我爲了工作而放了一個數字。刷新Webclient獲取新數據

解決方案是什麼?

void myButton_Click(object sender, RoutedEventArgs e) 
    { 
     i = 0; 
     lstService.Items.Clear(); 
     lstDestination.Items.Clear(); 
     listTime.Items.Clear(); 


     if (textSearchBox.Text == "Cranleigh Avenue") 
     { 
      textBlock2.Text = textSearchBox.Text; 
      txtstopId.Text = "6445"; 
     } 

     if (textSearchBox.Text == "Cranleigh Avenue (briamaw)") 
     { 
      textBlock2.Text = textSearchBox.Text; 
      txtstopId.Text = "7099"; 
     }     

     else if (textSearchBox.Text == "Brighton Station (stop A)") 
     { 
      textBlock2.Text = textSearchBox.Text; 
      txtstopId.Text = "7052"; 
     } 

     else if (textSearchBox.Text == "Brighton Station (stop B)") 
     { 
      textBlock2.Text = textSearchBox.Text; 
      txtstopId.Text = "7053"; 
     } 


     else if (textSearchBox.Text == "Brighton Station (stop C)") 
     { 
      textBlock2.Text = textSearchBox.Text; 
      txtstopId.Text = "7054"; 
     } 


     else if (textSearchBox.Text == "Brighton Station (stop D)") 
     { 
      textBlock2.Text = textSearchBox.Text; 
      txtstopId.Text = "7055"; 
     } 

     else if (textSearchBox.Text == "Sea Life Centre (stop K)") 
     { 
      textBlock2.Text = textSearchBox.Text; 
      txtstopId.Text = "7642"; 
     } 

     else if (textSearchBox.Text == "Race Hill") 
     { 
      textBlock2.Text = textSearchBox.Text; 
      txtstopId.Text = "6724"; 

     } 



     string myvar = txtstopId.Text; 
     textSearchBox.Text = ""; 
     try 
     { 
      WebClient webClient = new WebClient(); 
      Uri uri = new Uri("http://www.URL.aspx?stopid=" + HttpUtility.UrlEncode(myvar)); 
      webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); 
      webClient.OpenReadAsync(uri); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 
    public int i; 
    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
    { 
     DataContractJsonSerializer ser = null; 



     try 
     { 


      ser = new DataContractJsonSerializer(typeof(RootContainer)); 
      RootContainer rootContainer = ser.ReadObject(e.Result) as RootContainer; 
      foreach (Departures em in rootContainer.Departures) 
      { 
       i = i + 1; 

       if (i < 9) 
       { 


        string id = em.ServiceName; 
        string dt = em.Destination; 
        string tm = em.DepartureTimeAsString; 
        lstService.Items.Add(id); 
        lstDestination.Items.Add(dt); 
        listTime.Items.Add(tm); 



       } 
      } 
     } 


     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

    } 

編輯

WebClient webClient = new WebClient(); 
      string url = string.Format("http://www.URL.aspx?stopid={0}&ts={1}", HttpUtility.UrlEncode(myvar) + DateTime.Now.Ticks);     
      Uri uri = new Uri(url); 
      webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); 
      webClient.OpenReadAsync(uri); 

回答

3

我會使用時間戳不是一個隨機數:

string url = string.Format("http://www.URL.aspx?stopid={0}&ts={1}", 
     HttpUtility.UrlEncode(myvar), 
     DateTime.Now.Ticks); 
Uri uri = new Uri(url); 
+0

,當我在上面替換此,它拋出一個formatexpection 'WebClient的Web客戶端=新的Web客戶端(); string url = string.Format(「http://www.URL.aspx?stopid = {0}&ts = {1}」,HttpUtility.UrlEncode(myvar)+ DateTime.Now.Ticks); Uri uri = new Uri(url); webClient.OpenReadCompleted + = new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); webClient.OpenReadAsync(uri); ' 編輯 - 只是加了它 – 2012-04-03 15:02:43

+0

你可以把它放在主要問題。它格式化的事情更容易。 – gbanfill 2012-04-03 15:04:28

+0

它應該是myVar和DAteTime.Now.Ticks之間的逗號,格式爲:string.Format(「url?stopid = {0}&ts = {1}」,HttpUtility.UrlEncode(myvar),DateTime.Now.Ticks) ; – gbanfill 2012-04-03 15:07:47