“BACKGROUNDTASKHOST.EXE”退出代码为1(0x1)- 地理围栏问题

6
我正在使用GeoFence API编写Windows Phone 8.1应用程序,但我的问题是在后台任务中无法触发位置更改,因为应用程序以代码1退出。
我已经阅读了多个有关此错误的线程,但没有解决方案解决了我的问题。
  • 我已检查我的BackgroundTask是否为运行时组件,并且确实是。
  • 我已检查我的类的名称并且是正确的。
  • 我已检查我的BackgroundTask函数是否使用任何await函数,并且没有找到任何。
  • 我已检查我是否在应用程序清单中注册了Background Task,并且是的(当然是入口点)
事实上,在运行BackgroundTask的Run函数之前就出现了错误。
    namespace BackgroundTask
{
    public sealed class geoFenceBackgroundTask : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
            XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);

            XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
            toastTextElements[0].AppendChild(toastXml.CreateTextNode("MY APP"));
            toastTextElements[1].AppendChild(toastXml.CreateTextNode("Test"));

            //IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
            //XmlElement audio = toastXml.CreateElement("audio");

            ToastNotification toast = new ToastNotification(toastXml);
            ToastNotificationManager.CreateToastNotifier().Show(toast);
        }
    }
}

我的注册功能如下所示:
    async private void RegisterBackgroundTask()
        {
            // Get permission for a background task from the user. If the user has already answered once,
            // this does nothing and the user must manually update their preference via PC Settings.
            BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();

            // Regardless of the answer, register the background task. If the user later adds this application
            // to the lock screen, the background task will be ready to run.
            // Create a new background task builder
            BackgroundTaskBuilder geofenceTaskBuilder = new BackgroundTaskBuilder();

            geofenceTaskBuilder.Name = "geoFenceBackgroundTask";
            geofenceTaskBuilder.TaskEntryPoint = "BackgroundTask.geoFenceBackgroundTask";

            // Create a new location trigger
            var trigger = new LocationTrigger(LocationTriggerType.Geofence);

            // Associate the locationi trigger with the background task builder
            geofenceTaskBuilder.SetTrigger(trigger);

            // If it is important that there is user presence and/or
            // internet connection when OnCompleted is called
            // the following could be called before calling Register()
            // SystemCondition condition = new SystemCondition(SystemConditionType.UserPresent | SystemConditionType.InternetAvailable);
            // geofenceTaskBuilder.AddCondition(condition);

            // Register the background task

            var geofenceTask = geofenceTaskBuilder.Register();
            geofenceTask.Completed += (sender, args) =>
            {
// MY CODE HERE

            };

            geofenceTask = geofenceTaskBuilder.Register();            
        }

我没有其他的想法。能帮忙吗?

2个回答

7

我刚刚遇到了类似的问题(当我在“生命周期事件下拉列表”上启动后台任务时,Visual Studio停止调试,并在控制台输出窗口中声明:“BACKGROUNDTASKHOST.EXE已以代码1(0x1)退出”。)。

在WP8项目中添加缺失的任务程序集(winmd)工程参考解决了这个问题。


你能稍微解释一下你的答案吗?我遇到了完全相同的问题。 - Joseph Freeman
1
遇到了同样的问题。请点击以下链接,了解如何添加项目引用 https://msdn.microsoft.com/zh-cn/library/hh708954.aspx 你的运行时组件必须在你的Windows Phone项目的“引用”下列出。 - Ascot Harris

0

项目 BackgroundTask 需要成为 Windows 运行时组件。


1
目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接