2013-07-11 134 views
0

我正在嘗試創建一個後臺代理程序,以定期更新Windows Phone上的用戶活動瓷磚。活動瓷磚未更新

目前,我的代理代碼是:

string where = ""; 
    private GeoCoordinate MyCoordinate = null; 
    HttpWebResponse webResponse; 
    ... 
    protected override void OnInvoke(ScheduledTask task) 
    { 
     System.Diagnostics.Debug.WriteLine("Invoked"); 
     findMe(); 

     NotifyComplete(); 
    } 

    private void ResponseCallback(IAsyncResult asyncResult) 
    { 
     HttpWebRequest webRequest = (HttpWebRequest)asyncResult.AsyncState; 
     webResponse = (HttpWebResponse)webRequest.EndGetResponse(asyncResult); 

     MemoryStream tempStream = new MemoryStream(); 
     webResponse.GetResponseStream().CopyTo(tempStream); 
    } 

    private async void findMe() 
    { 
     Geolocator geolocator = new Geolocator(); 
     geolocator.DesiredAccuracy = PositionAccuracy.High; 

     try 
     { 
      Geoposition currentPosition = await geolocator.GetGeopositionAsync(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10)); 

      MyCoordinate = new GeoCoordinate(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude); 

      // var uri = new Uri("http://www.streetdirectory.com//api/?mode=nearby&act=location&output=json&callback=foo&start=0&limit=1&country=sg&profile=template_1&x=" + MyCoordinate.Longitude + "&y=" + MyCoordinate.Latitude + "&dist=1"); 
      // var client = new HttpClient(); 

      var webRequest = (HttpWebRequest)HttpWebRequest.CreateHttp("http://www.streetdirectory.com//api/?mode=nearby&act=location&output=json&callback=foo&start=0&limit=1&country=sg&profile=template_1&x=" + MyCoordinate.Longitude + "&y=" + MyCoordinate.Latitude + "&dist=1"); 
      webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), webRequest); 

      System.Diagnostics.Debug.WriteLine("findMe after response"); 
      System.Diagnostics.Debug.WriteLine(MyCoordinate.Latitude); 
      System.Diagnostics.Debug.WriteLine(MyCoordinate.Longitude); 
      // var response = await client.GetStringAsync(uri); 
      System.Diagnostics.Debug.WriteLine(webResponse.ToString()); 

      JToken token = JArray.Parse(webResponse.ToString())[0]; 
      // JToken token = JArray.Parse(response)[0]; 
      var name = token.Next.First.First; 
      var address = token.Next.Last.First; 
      where = name + ", " + address; 
     } 
     catch (Exception) 
     { 
      System.Diagnostics.Debug.WriteLine("findMe died"); 
      where = ""; 
     } 
     System.Diagnostics.Debug.WriteLine("findMe complete"); 
     UpdateAppTile(); 
    } 

    private void UpdateAppTile() 
    { 
     System.Diagnostics.Debug.WriteLine("UpdateAppTile"); 
     ShellTile appTile = ShellTile.ActiveTiles.First(); 
     if (appTile != null) 
     { 
      StandardTileData tileData = new StandardTileData 
      { 
       BackContent = where 
      }; 

      appTile.Update(tileData); 
     } 
     System.Diagnostics.Debug.WriteLine("Update Completed: " + where); 
    } 

當我嘗試運行此,代碼達到webRequest.BeginGetResponse,隨後停止。下一行,並且ResponseCallback未到達。

我的代碼的舊版本被註釋掉了,我認爲這是問題,但也遇到了同樣的問題。

回答

3

問題是您在回調返回之前調用NotifyComplete()

致電NotifyComplete您告訴操作系統您已完成所有工作並且代理可以終止。當你等待webrequest回調時,顯然情況並非如此。

簡單的解決方案是將此調用移入回調方法。顯然你需要處理錯誤異常,並且請求超時比代理等待的時間要長。

更改爲使用等待代碼可能會使您更容易。

+0

就是這樣!非常感謝! :d – ReignOfComputer