我正在努力注册后台任务,因为我想等待的方法仍然被调用..

我还得到错误“Class not registered Exception”,但是通过在Declarations >> BackgroundTasks下的清单中添加我的TaskBuilder类作为入口点来解决它 . 不知道这是不是正确的事情..但它的工作.. :)

这就是我现在用来构建任务的东西:

public async void RegisterBackgroundTask()
    {
        var tommorowMidnight = DateTime.Today.AddDays(1);
        var timeTilMidnight = tommorowMidnight - DateTime.Now;
        var minutesTilMidnight = (uint)timeTilMidnight.TotalMinutes;

        var task = RegisterBackgroundTask("TaskBuilder",
                                          "TimeTriggeredTask",
                                          new TimeTrigger(minutesTilMidnight, false),
                                          null);
        await task;
        CheckPremieres(); //My method that i want to check only once a day at 00:00
    }

public static async Task<BackgroundTaskRegistration> RegisterBackgroundTask(String taskEntryPoint, String name, IBackgroundTrigger trigger, IBackgroundCondition condition)
        {

            await BackgroundExecutionManager.RequestAccessAsync();

            var builder = new BackgroundTaskBuilder();

            builder.Name = name;
            builder.TaskEntryPoint = taskEntryPoint;
            builder.SetTrigger(trigger);

            BackgroundTaskRegistration task = builder.Register();

            return task;
        }

这是TaskBuilder类:

class TaskBuilder : IBackgroundTask
    {
        BackgroundTaskCancellationReason _cancelReason = BackgroundTaskCancellationReason.Abort;
        volatile bool _cancelRequested = false;
        BackgroundTaskDeferral _deferral = null;
        ThreadPoolTimer _periodicTimer = null;
        uint _progress = 0;
        IBackgroundTaskInstance _taskInstance = null;

        //
        // The Run method is the entry point of a background task.
        //
        public void Run(IBackgroundTaskInstance taskInstance)
        {            
            //
            // Query BackgroundWorkCost
            // Guidance: If BackgroundWorkCost is high, then perform only the minimum amount
            // of work in the background task and return immediately.
            //
            var cost = BackgroundWorkCost.CurrentBackgroundWorkCost;
            var settings = ApplicationData.Current.LocalSettings;
            settings.Values["BackgroundWorkCost"] = cost.ToString();

            //
            // Associate a cancellation handler with the background task.
            //
            taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);

            //
            // Get the deferral object from the task instance, and take a reference to the taskInstance;
            //
            _deferral = taskInstance.GetDeferral();
            _taskInstance = taskInstance;

            _periodicTimer = ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler(PeriodicTimerCallback), TimeSpan.FromSeconds(1));
        }

        //
        // Handles background task cancellation.
        //
        private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
        {
            //
            // Indicate that the background task is canceled.
            //
            _cancelRequested = true;
            _cancelReason = reason;
        }

        //
        // Simulate the background task activity.
        //
        private void PeriodicTimerCallback(ThreadPoolTimer timer)
        {
            if ((_cancelRequested == false) && (_progress < 100))
            {
                _progress += 10;
                _taskInstance.Progress = _progress;
            }
            else
            {
                _periodicTimer.Cancel();

                var settings = ApplicationData.Current.LocalSettings;
                var key = _taskInstance.Task.Name;

                //
                // Write to LocalSettings to indicate that this background task ran.
                //
                settings.Values[key] = (_progress < 100) ? "Canceled with reason: " + _cancelReason.ToString() : "Completed";

                //
                // Indicate that the background task has completed.
                //
                _deferral.Complete();
            }
        }
    }

我使用MS在GitHub上的示例:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundTask

这是我过去使用的延迟方法调用的方法:

public void SetTimeToShowNotification(DateTime date)
        {
            var dateNow = DateTime.Now;
            TimeSpan ts;
            if (date > dateNow)
                ts = date - dateNow;
            else
            {
                date = getNextDate(date);
                ts = date - dateNow;
            }
            //waits certan time and run the code
            Task.Delay(ts).ContinueWith((x) =>
            {
                //run the code at the time
                CheckPremieres();

                //setup call next day
                SetTimeToShowNotification(getNextDate(date));

            });
        }
        private DateTime getNextDate(DateTime date)
        {
            if (App.CheckOnce)
            {
                return date.AddSeconds(20);
            }
            return date.AddDays(1);
        }

但是这会消耗电池我认为并且在后台工作...

我想要做的就是每天运行一个方法向用户发送通知..那不可能是那个dificullt但我无法让它工作..