现在的位置: 首页 > 综合 > 正文

WP7在使用后台计划任务代理的时候要注意的地方

2012年11月14日 ⁄ 综合 ⁄ 共 1147字 ⁄ 字号 评论关闭
在 Windows Phone OS 7.1 中,用户能够在手机的“设置”应用程序中为安装的每个应用程序禁用后台代理。如果已为您的应用程序禁用代理,则尝试使用 ScheduledActionService 的 Add(ScheduledAction) 方法将引发 InvalidOperationException,因此将对 Add 的调用放在 try 块中。如果引发异常并且与异常关联的消息为“BNS Error: The action is disabled(BNS 错误: 该操作已被禁用)”,则表示已为您的应用程序禁用代理。以下代码演示了处理这种情况的正确方法。

 

PeriodicTask periodicTask = new PeriodicTask("MyPeriodicTask");
periodicTask.Description = "This demonstrates a periodic task.";

// Place the call to Add in a try block in case the user has disabled agents.
try
{
  ScheduledActionService.Add(periodicTask);
}
catch (InvalidOperationException exception)
{
  if (exception.Message.Contains("BNS Error: The action is disabled"))
  {
    MessageBox.Show("后台代理已被用户禁用!");
    agentsAreEnabled = false;
  }
  if (exception.Message.Contains("BNS Error: The maximum number of ScheduledActions of this type have already been added."))
  {
    // 后台代理的数量达到设备限制
  }
}
catch (SchedulerServiceException)
{
  // 在设备刚刚启动而计划操作服务尚未启动的情况下
}

 

#define DEBUG_AGENT

using.........
..
.
.

#if DEBUG_AGENT
  ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(60));
#endif

//此方法仅用于应用程序开发目的。除了在使用开发工具部署的应用程序中
//之外,此方法不起任何作用。您应该从生产应用程序中删除对此方法的
//调用。在此示例中,该调用位于 #if 块中,以便您能够在调试和生产功
//能之间轻松切换。

 

 

在调试期间如果出现了如图的异常,看看是否选择启动的Emulator是否是256M的或者不是完整版的,尝试使用完整版的Emulator(传送门)

抱歉!评论已关闭.