我正在开发一个 Windows Phone 应用程序,它可以随时间更新磁贴的HH:mm:ss
格式。我已经使用 PeriodicTask 进行了测试,它在调试条件下更新磁贴 30 分钟和 30 秒。所以,我发现 ResourceIntensiveTask 对我来说似乎很有用,当满足某些条件时,它们会尽快更新(我从http://jesseliberty.com/2011/10/05/background-agents/获得了这些知识),所以我有在 ScheduledAgent.cs 中编程如下
protected override void OnInvoke(ScheduledTask task)
{
StandardTileData data = new StandardTileData
{
Title = DateTime.Now.ToString("hh:mm:ss"),
//BackgroundImage = new Uri("/Background.png", UriKind.RelativeOrAbsolute)
};
ShellTile.ActiveTiles.First().Update(data);
#if DEBUG_AGENT
ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromMilliseconds(1));
#endif
NotifyComplete();
}
和 MainPage.xaml.cs 作为
public MainPage()
{
InitializeComponent();
agentsAreEnabled = true;
periodicTask = ScheduledActionService.Find(periodicTaskName) as ResourceIntensiveTask;
if (periodicTask != null)
{
RemoveAgent(periodicTaskName);
}
periodicTask = new ResourceIntensiveTask(periodicTaskName);
periodicTask.Description = "This checks for audio jack plugged in task.";
try
{
ScheduledActionService.Add(periodicTask);
#if(DEBUG_AGENT)
// If debugging is enabled, use LaunchForTest to launch the agent in one minute.
ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromMilliseconds(1));
#endif
}
catch (InvalidOperationException exception)
{
if (exception.Message.Contains("BNS Error: The action is disabled"))
{
MessageBox.Show("Background agents for this application have been disabled by the user.");
agentsAreEnabled = false;
}
if (exception.Message.Contains("BNS Error: The maximum number of ScheduledActions of this type have already been added."))
{
// No user action required. The system prompts the user when the hard limit of periodic tasks has been reached.
}
}
catch (SchedulerServiceException)
{
// No user action required.
}
}
请注意,我正在使用TimeSpan.FromMilliseconds(1)
而不是TimeSpan.FromSeconds(30)
在代码的任何位置显示时间,现在当我运行应用程序并固定磁贴时,它会立即以 HH:mm:ss 格式显示时间,但不会更新。这里可能是什么问题。我在正确的轨道上吗。
请记住,我的设备满足运行 ResourceIntensiveTask 的所有条件,我也从这里指的是他们提到的ScheduledActionService.LaunchForTest(taskName, TimeSpan.FromMilliseconds(10));