2012-01-09 21 views
2

我試圖從我的Twitter帳戶中獲取推文,並在我的外殼磁貼上顯示最多的推文。所以我創建了一個後臺代理(定期任務)來完成它。每隔30分鐘,後臺代理應該訪問我的Twitter時間表並獲取我最頂級的推文並將其顯示在平鋪中。問題是我的瓷磚只有一次更新,即當我啓動代理時,之後它不會更新。Shelltile在Windows Phone 7.1中未正確更新

這裏是我的後臺代理代碼:

protected override void OnInvoke(ScheduledTask task) 
{ 
    ShellToast popupMessage = new ShellToast() 
    { 
     Title = "My First Agent", 
     Content = "Background Task Launched", 
    }; 

    WebClient twitter = new WebClient(); 
    twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted); 
    twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=dnivra26")); 
    popupMessage.Show(); 
} 

private void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
{ 
    if (e.Error != null) 
     return; 

    XElement xmlTweets = XElement.Parse(e.Result); 

    var message2 = (from tweet in xmlTweets.Descendants("status") 
        select tweet.Element("text").Value).FirstOrDefault(); 

    UpdateAppTile(DateTime.Now.ToString() + message2.ToString()); 
} 

private void UpdateAppTile(string message) 
{ 
    ShellTile appTile = ShellTile.ActiveTiles.First(); 
    if (appTile != null) 
    { 
     StandardTileData tileData = new StandardTileData 
     { 
      BackContent = message 
     }; 

     appTile.Update(tileData); 
     //NotifyComplete(); 
    } 
} 

我能夠獲得最高鳴叫一次。

回答

2

完成後,您必須調用NotifyComplete()。如果不是,任務的調度將被中止。你爲什麼評論它?

+1

我是慢應答。 :D – BigL 2012-01-09 21:20:17

2

我從來沒有嘗試過,但這看起來像一個很好的例子來編寫一個定期的後臺代理。

我的猜測是,你應該調用NotifyComplete()結尾,它告訴操作系統,你的任務是準備好了。

Periodical Agent on Windows Phone 7

1

正如其他人所說,你需要調用NotifyComplete()完成後。但是,由於您使用的是異步事件WebClient.DownloadStringCompleted,因此需要鎖定執行直到下載字符串完成。

對於這一點,我建議使用Task Parallel Library for Silverlight

你需要做的,是這樣的:

protected override void OnInvoke(ScheduledTask task) 
{ 
    ShellToast popupMessage = new ShellToast() 
    { 
     Title = "My First Agent", 
     Content = "Background Task Launched", 
    }; 
    popupMessage.Show(); 

    UpdateTile().ContinueWith(x => NotifyComplete()); 
} 

private Task<bool> UpdateTile() 
{ 
    var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.AttachedToParent); 

    WebClient twitter = new WebClient(); 

    twitter.DownloadStringCompleted += (sender, e) => 
    { 
     if (e.Error != null) 
     { 
      tcs.TrySetResult(true); 
     } 
     else 
     { 
      XElement xmlTweets = XElement.Parse(e.Result); 

      var message2 = xmlTweets.Descendants("status") 
            .Select(x => x.Element("text").Value).FirstOrDefault(); 

      ShellTile appTile = ShellTile.ActiveTiles.First(); 

      if (appTile != null) 
      { 
       StandardTileData tileData = new StandardTileData 
       { 
        BackContent = DateTime.Now.ToString() + message2.ToString() 
       }; 

       appTile.Update(tileData); 

       tcs.TrySetResult(true); 
      } 
      else 
      { 
       tcs.TrySetResult(true); 
      } 
     } 
    }; 

    twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=dnivra26")); 

    return tcs.Task; 
} 
+0

如何在「visual studio 2010 express for windows phone」中安裝System.Threading.Tasks包。我是Windows Mobile開發新手。請幫助我解決這個問題。 – dnivra 2012-01-10 07:57:48

+0

http://docs.nuget.org/docs/start-here/installing-nuget – 2012-01-10 08:00:02

+0

我安裝了軟件包並按照您的說明進行了更改。但是這行「var tcs = new TaskCompletionSource (TaskCreationOptions.AttachedToParent);」顯示「UnauthorizedAccessException」。我現在要做什麼? – dnivra 2012-01-10 10:41:50