PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/OSChinaScheduledTask_Notice/ScheduledAgent.cs

https://github.com/Septemberd/wp7-app
C# | 116 lines | 80 code | 6 blank | 30 comment | 8 complexity | 05efac19fdf0b623518631fdafac1006 MD5 | raw file
Possible License(s): GPL-2.0
  1. using System;
  2. using System. Linq;
  3. using System. Net;
  4. /*
  5. * 原作者: 王俊
  6. *
  7. */
  8. using System. Windows;
  9. using System. Xml. Linq;
  10. using Microsoft. Phone. Scheduler;
  11. using Microsoft. Phone. Shell;
  12. namespace OSChinaScheduledTask_Notice
  13. {
  14. public class ScheduledAgent : ScheduledTaskAgent
  15. {
  16. #region 固定不变的原始代码
  17. private static volatile bool _classInitialized;
  18. /// <remarks>
  19. /// ScheduledAgent 构造函数,初始化 UnhandledException 处理程序
  20. /// </remarks>
  21. public ScheduledAgent( )
  22. {
  23. if ( !_classInitialized )
  24. {
  25. _classInitialized = true;
  26. // 订阅托管的异常处理程序
  27. Deployment. Current. Dispatcher. BeginInvoke( delegate
  28. {
  29. Application. Current. UnhandledException += ScheduledAgent_UnhandledException;
  30. } );
  31. }
  32. }
  33. /// 出现未处理的异常时执行的代码
  34. private void ScheduledAgent_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
  35. {
  36. if ( System. Diagnostics. Debugger. IsAttached )
  37. {
  38. // 出现未处理的异常;强行进入调试器
  39. System. Diagnostics. Debugger. Break( );
  40. }
  41. }
  42. #endregion
  43. /// <summary>
  44. /// 執行排程工作的代理程式
  45. /// </summary>
  46. /// <param name="task">
  47. /// 叫用的工作
  48. /// </param>
  49. /// <remarks>
  50. /// 這個方法的呼叫時機為叫用週期性或耗用大量資料的工作時
  51. /// </remarks>
  52. protected override void OnInvoke(ScheduledTask task)
  53. {
  54. string[ ] parties = task. Description. Split( '@' );
  55. //重新组装 cookie
  56. string cookie = string. Format( "oscid=''; Expires=Thu, 01-Jan-1970 00:00:10 GMT;{0}; Path=/", parties[ 0 ] );
  57. string uid = parties[ 1 ];
  58. this. Process_GetNotice( cookie, uid );
  59. }
  60. /// <summary>
  61. /// 根据cookie以及uid 来获取 UserNotice
  62. /// </summary>
  63. /// <param name="cookie">cookie</param>
  64. /// <param name="uid">用户uid</param>
  65. private void Process_GetNotice(string cookie, string uid)
  66. {
  67. WebClient client = new WebClient( );
  68. client. Headers[ "Cookie" ] = cookie;
  69. client. DownloadStringCompleted += (s, e) =>
  70. {
  71. if ( e.Error == null )
  72. {
  73. XElement root = XElement. Parse( e.Result );
  74. XElement notice = root. Element( "notice" );
  75. if ( notice == null )
  76. {
  77. return;
  78. }
  79. else
  80. {
  81. int a = int. Parse( notice. Element( "atmeCount" ). Value );
  82. int b = int. Parse( notice. Element( "msgCount" ). Value );
  83. int c = int. Parse( notice. Element( "reviewCount" ). Value );
  84. int d = int. Parse( notice. Element( "newFansCount" ). Value );
  85. //更新个数
  86. UpdateMainTile( a + b + c + d );
  87. }
  88. }
  89. };
  90. string uri = string. Format( "http://www.oschina.net/action/api/user_notice?uid={0}&guid={1}", uid, Guid. NewGuid( ). ToString( ) );
  91. client. DownloadStringAsync( new System. Uri( uri, System. UriKind. Absolute ) );
  92. }
  93. /// <summary>
  94. /// 更新App 的主贴片
  95. /// </summary>
  96. /// <param name="noticeCount">通知个数</param>
  97. private void UpdateMainTile(int noticeCount)
  98. {
  99. var appTile = ShellTile. ActiveTiles. FirstOrDefault( );
  100. if ( appTile != null )
  101. {
  102. StandardTileData firstTile = new StandardTileData
  103. {
  104. Count = noticeCount
  105. };
  106. appTile. Update( firstTile );
  107. }
  108. }
  109. }
  110. }