我在JavasScipt中構建UWP應用程序。
本細則我下面:UWP:如何在BackgroundTask中執行操作
Create and register an out-of-process background task
Support your app with background tasks
這裏是BackgroundTask初始化:
(function() {
"use strict"
// Initialization
var Background = Windows.ApplicationModel.Background;
// Register
var taskBuilder = new Background.BackgroundTaskBuilder();
taskBuilder.name = "MyBackgroundTask";
// init trigger
var appTrigger = new Background.ApplicationTrigger();
taskBuilder.taskEntryPoint = "clipboardListener.MyBackgroundTask";
taskBuilder.setTrigger(appTrigger);
// Check if background task already registered
var taskRegistered = false;
var iter = Background.BackgroundTaskRegistration.allTasks.first();
var hascur=iter.hasCurrent;
while (hascur)
{
var current = iter.current.value;
if (current.name == "MyBackgroundTask")
{
taskRegistered = true;
break;
}
iter.moveNext();
}
if (!taskRegistered)
{
var task = taskBuilder.register();
}
})();
下面是我設置的任務:
function runBackgroundTask() {
appTrigger.requestAsync().then(function (result) {
closeRun();
}, function (err) {
console.log(err);
});
};
如您所見,我在上面的代碼中設置了appTrigger
變量來設置任務的觸發器。
我需要觸發後臺任務,每當用戶複製的東西給他的剪貼板?
如何在後臺任務運行時執行某些操作?