1

我正在尝试为我的 JavaScript Metro 应用程序创建一个后台任务。我将这些添加到default.js文件中:

function RegisterBackgroundTask(taskEntryPoint, taskName, trigger, condition) {
    // Check for existing registrations of this background task.
    var taskRegistered = false;
    var background = Windows.ApplicationModel.Background;
    var iter = background.BackgroundTaskRegistration.allTasks.first();
    var hascur = iter.hasCurrent;

    while (hascur) {
        var cur = iter.current.value;
        if (cur.name === taskName) {
            taskRegistered = true;
            break;
        }
        hascur = iter.moveNext();
    }

    // If the task is already registered, return the registration object.
    if (taskRegistered == true) {
        return iter.current;
    }

    // Register the background task.
    var builder = new background.BackgroundTaskBuilder();

    builder.Name = taskName;
    builder.TaskEntryPoint = taskEntryPoint;
    builder.setTrigger(trigger);

    if (condition != null) {
        builder.addCondition(condition);
    }
    var task = builder.register();
    return task;
}

var trigger = new Windows.ApplicationModel.Background.SystemTrigger(Windows.ApplicationModel.Background.SystemTriggerType.timeZoneChange, false);

RegisterBackgroundTask("js\\bgtask.js", "test", trigger);

这是我的bgtask.js

(function () {
    "use strict";
    var backgroundTaskInstance = Windows.UI.WebUI.WebUIBackgroundTaskInstance.current;

    function doWork() {

        // Write JavaScript code here to do work in the background.
        console.log("task done");

        close();
    }

    doWork();
})();

这是我的应用清单:

在此处输入图像描述

当我更改时区时,什么也没发生。我检查了事件日志,有一个错误:

具有入口点和名称的后台任务无法激活,错误代码为 0x80040154。

我做错了什么?

4

1 回答 1

1
builder.Name = taskName;
builder.TaskEntryPoint = taskEntryPoint;

应该

builder.name = taskName;
builder.taskEntryPoint = taskEntryPoint;
于 2014-08-07T03:11:00.553 回答