Windows Phone 8.1 运行时 - 后台任务时间触发器
本文关键字:后台任务 时间 触发器 运行时 Phone Windows | 更新日期: 2023-09-27 17:57:13
我正在尝试在日历条目的结束时间触发后台任务。例如,如果日历条目是下午 3 点 - 5 点,我希望后台任务在下午 5 点触发,我还想根据即将到来的日历条目设置另一个时间触发器。在日历条目时间结束时,我将通过查询数据库来获取有关下一个日历条目的详细信息,并将结束时间设置为下一个时间触发器。我不确定如何设置时间,因为每次都会有所不同。到目前为止,这就是我要做的:
//Background Task updated for Live Tile
Windows.Storage.ApplicationDataContainer pageData = Windows.Storage.ApplicationData.Current.LocalSettings;
pageData.Values["Testing"] = DateTime.Now.ToString();
bool taskRegistered = false;
string UpdateTile = "UpdateTile";
// check if task is already registered
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == UpdateTile)
{
await (new MessageDialog("Task already registered")).ShowAsync();
taskRegistered = true;
break;
}
}
// register a new task
var builder = new BackgroundTaskBuilder();
builder.TaskEntryPoint = "TileUpdaterTask.UpdateTile";
builder.SetTrigger(new TimeTrigger(30, false));
// Windows Phone app must call this to use trigger types (see MSDN)
await BackgroundExecutionManager.RequestAccessAsync();
BackgroundTaskRegistration theTask = builder.Register();
下面是用于更新动态磁贴的 Windows 运行时组件类:
public sealed class UpdateTile : IBackgroundTask //XamlRenderingBackgroundTask
{
Windows.Storage.ApplicationDataContainer pageData = Windows.Storage.ApplicationData.Current.LocalSettings;
public void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
ITileSquare150x150Text01 tileContent = TileContentFactory.CreateTileSquare150x150Text01();
tileContent.TextHeading.Text = "Hello!";
tileContent.TextBody1.Text = pageData.Values["Testing"].ToString();//"One";
tileContent.TextBody2.Text = "Two";
tileContent.TextBody3.Text = "Three";
TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());
_deferral.Complete();
}
}
请指教。
从头开始:
- 创建新的 Windows Phone RT 应用解决方案
- 向解决方案添加新的 Windows RT 组件项目
- 将以下代码放在后台任务的主类中。这段代码被设计为大约每n分钟(30分钟的倍数)"做某事"(与你要求的略有不同,但你应该明白这个想法)。它从本地设置中读取两个值 - 时间戳和间隔时间(以分钟为单位)。这些值在主应用程序中设置。
法典:
using System;
using System.Globalization;
using Windows.ApplicationModel.Background;
using Windows.Storage;
namespace WindowsRuntimeComponent1
{
public sealed class MyBackgroundClass : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
// Get a deferral, to prevent the task from closing prematurely if asynchronous code is still running.
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
// GET THIS INFO
ApplicationDataContainer LocalSettings = ApplicationData.Current.LocalSettings;
double elapsed = (DateTime.Now - Convert.ToDateTime(LocalSettings.Values["time stamp"], CultureInfo.InvariantCulture)).TotalMinutes;
double remainingMinutes = (int) LocalSettings.Values["gap time minutes"] - elapsed;
// SEE IF WE CAN DO IT NOW
if (remainingMinutes > 15)
{
// do something, otherwise, wait for the next one
}
// FINISH UP
deferral.Complete();
}
}
}
- 在主应用程序中,转到 Package.appxmanifest 文件
- 在声明选项卡中,添加类型为"计时器"的"后台任务"
- 入口点应为"WindowsRuntimeComponent1.MyBackgroundClass"
- 在主应用的 Mainpage.xaml.cs 中,将以下代码插入构造函数。
法典:
// UNREGISTER THE EXISTING TASK IF IT EXISTS
var myTask = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault(iTask => iTask.Name == "DoSomethingBackground");
if (myTask != null) myTask.Unregister(true);
// REGISTER NEW TASK TO EXECUTE EVERY 30 MINUTES
BackgroundTaskBuilder myTaskBuilder = new BackgroundTaskBuilder() { Name = "DoSomethingBackground", TaskEntryPoint = "WindowsRuntimeComponent1.MyBackgroundClass" };
myTaskBuilder.SetTrigger(new TimeTrigger(30, false));
myTaskBuilder.Register();
// CREATE/RESET THESE VARIABLES
ApplicationData.Current.LocalSettings.Values["gap time minutes"] = 60;
ApplicationData.Current.LocalSettings.Values["time stamp"] = DateTime.Now.ToString(CultureInfo.InvariantCulture);
- 在调试模式下为后台任务生成项目并添加引用...WindowsRuntimeComponent1''bin''Debug''WindowsRuntimeComponent1.winmd 到主应用程序。
- 在调试中运行主应用程序。从顶部菜单中的生命周期事件中,选择"DoSomethingBackground"并运行它。在 MyBackgroundClass 中放置一个断点.cs以查看它是否正确执行。
您可能会遇到这样的情况:当您尝试从生命周期事件运行"DoSomethingBackground"时,程序会突然结束。如果发生这种情况,请删除主应用的 bin 和 obj 文件,重新生成所有内容,然后重新附加 .winmd 文件。这应该让它再次工作。此外,最好在后台任务中大量使用 Debug.WriteLine(),因此当您执行它时,您可以在输出窗口中跟踪您的代码在做什么。 最后,小心使用日期时间格式,由于全局格式不同,我建议您在存储日期时使用 CultureInfo.InvariantCulture。