win10通用应用程序后台任务未运行

时间:2016-01-07 03:18:34

标签: c# windows-10-universal

我有一个剥离的Windows 10通用应用程序(VS2015),后台任务应该更新实时磁贴,但它似乎不起作用。我创建了一个带有两个按钮的基本XAML页面:“启用”和“禁用”。启用按钮注册后台任务,禁用按钮注销后台任务。

主应用程序(App6)MainPage.cs文件的代码:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace App6
{
    public sealed partial class MainPage : Page
    {
        private string TN = "APP6_TASK";
        private string TEP = "App6BackgroundTask2.BackgroundTaskHelper";

        public MainPage()
        {
            InitializeComponent();
        }

        private void Enable(object sender, RoutedEventArgs e)
        {
            try { RegisterTask(); }
            catch(Exception ex) { ShowDialog(ex.ToString()); }
        }

        private void Disable(object sender, RoutedEventArgs e)
        {
            try { UnregisterTask(); }
            catch (Exception ex) { ShowDialog(ex.ToString()); }
        }

        private async void ShowDialog(string message)
        {
            var dlg = new MessageDialog(message);
            await dlg.ShowAsync();
        }

        public void RegisterTask()
        {
            //no need to register a task that already exists
            if (GetTask()) { return; }

            var backgroundTaskBuilder = new BackgroundTaskBuilder() { Name = TN, TaskEntryPoint = TEP };
            backgroundTaskBuilder.SetTrigger(new TimeTrigger(15, false));
            backgroundTaskBuilder.Register();
        }

        public void UnregisterTask()
        {
            var task = new KeyValuePair<Guid, IBackgroundTaskRegistration>();

            //no need to unregister a task that doesn't exist
            if (GetTask(ref task))
            {
                task.Value.Unregister(true);
            }
        }

        public bool GetTask()
        {
            foreach (var t in BackgroundTaskRegistration.AllTasks)
            {
                if (t.Value.Name == TN)
                {
                    return true;
                }
            }
            return false;
        }

        public bool GetTask(ref KeyValuePair<Guid, IBackgroundTaskRegistration> task)
        {
            foreach (var t in BackgroundTaskRegistration.AllTasks)
            {
                if (t.Value.Name == TN)
                {
                    task = t;
                    return true;
                }
            }
            return false;
        }
    }
}

我打开了Package.AppManifest文件,在“声明”选项卡下添加了“后台任务”声明,并选择了“定时器”任务类型。对于我输入的入口点

App6BackgroundTask2.BackgroundTaskHelper

我在解决方案中添加了第二个项目,并从主应用程序中引用了它。该项目中唯一类的代码(BackgroundTaskHelper.cs)是:

using System;
using System.Diagnostics;
using Windows.ApplicationModel.Background;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;

namespace App6BackgroundTask2
{
    public sealed class BackgroundTaskHelper : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            var backgroundTaskDeferral = taskInstance.GetDeferral();
            try { UpdateTile(); }
            catch (Exception ex){ Debug.WriteLine(ex); }
            finally { backgroundTaskDeferral.Complete(); }
        }

        private void UpdateTile()
        {
            var now = DateTime.Now.ToString("HH:mm:ss");
            var xml =
                "<tile>" +
                "  <visual>" +
                "    <binding template='TileSmall'>" +
                "      <text>" + now + "</text>" +
                "    </binding>" +
                "    <binding template='TileMedium'>" +
                "      <text>" + now + "</text>" +
                "    </binding>" +
                "    <binding template='TileWide'>" +
                "      <text>" + now + "</text>" +
                "    </binding>" +
                "    <binding template='TileLarge'>" +
                "      <text>" + now + "</text>" +
                "    </binding>" +
                "  </visual>" +
                "</tile>";

            var tileDom = new XmlDocument();
            tileDom.LoadXml(xml);
            var tile = new TileNotification(tileDom);
            TileUpdateManager.CreateTileUpdaterForApplication().Update(tile);
        }
    }
}

我在调试模式下运行主应用程序,单击“启用”以注册后台任务,从Debug.Location工具栏暂停任务并验证任务的Run方法执行并且不会抛出异常。检查“开始”菜单时,磁贴会按预期更新。当我关闭应用程序并等待时,磁贴永远不会再次更新。

我卸载了应用程序,重新编译为Release并从开始菜单运行它(未在IDE中运行)。我点击“启用”按钮注册后台任务,关闭应用程序并等待。瓷砖从未更新。

看来我错过了一些东西,但我无法弄清楚是什么。任何帮助表示赞赏。感谢名单

1 个答案:

答案 0 :(得分:0)

我找到了答案。

我在package.appxmanifest的Declarations选项卡中选择了“System event”任务类型(除了“Timer”任务类型)。

我还为“应用程序”选项卡中的“锁定屏幕通知”选择框选择了“徽章和平铺文本”。

我在Visual Assets标签中为徽章徽标选择了“Assets \ LockScreenLogo.png”。

最后,我修改了我的注册任务例程

public async Task RegisterTask()
{
    var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
    if (backgroundAccessStatus == BackgroundAccessStatus.Denied) { return; }

    if (GetTask()) { return; }

    var timeTrigger = new TimeTrigger(15, false);

    var backgroundTaskBuilder = new BackgroundTaskBuilder();
    backgroundTaskBuilder.Name = TASK_NAME;
    backgroundTaskBuilder.TaskEntryPoint = typeof(BackgroundTileTimerTask.BackgroundTask).FullName;
    backgroundTaskBuilder.SetTrigger(timeTrigger);

    backgroundTaskBuilder.Register();
}

这似乎已经成功了。

我已经将代码和如何将PDF添加到GitHub:GitHub repository