C# BackgroundTaskBuilder寄存器()问题

C# BackgroundTaskBuilder寄存器()问题,c#,windows-8,background,windows-8.1,geofencing,C#,Windows 8,Background,Windows 8.1,Geofencing,我当时正在使用GeoFenging开发应用程序,但遇到了问题: ArgumentException(值不在预期范围内) 当我尝试注册地理围栏的后台任务时 来自MSDN的样本也有同样的问题。为了清晰起见,我将显示示例中的代码(链接到示例: 使用场景5, -说明如何测试) 我所做的一切: 在Visual Studio中构建我的应用程序 首先在本地部署应用程序,然后在“设置”中将应用程序添加到锁定屏幕 关闭正在本地运行的应用程序 在Visual Studio模拟器中启动应用程序 调用寄存器Backgr

我当时正在使用GeoFenging开发应用程序,但遇到了问题:

ArgumentException(值不在预期范围内) 当我尝试注册地理围栏的后台任务时

来自MSDN的样本也有同样的问题。为了清晰起见,我将显示示例中的代码(链接到示例: 使用场景5, -说明如何测试)

我所做的一切:

  • 在Visual Studio中构建我的应用程序
  • 首先在本地部署应用程序,然后在“设置”中将应用程序添加到锁定屏幕
  • 关闭正在本地运行的应用程序
  • 在Visual Studio模拟器中启动应用程序
  • 调用寄存器BackgroundTask(..)(场景5中只需按下寄存器按钮)
  • 我对MSDN示例中的一些代码进行了评论,这些代码在模拟器中成功部署了应用程序,并标记了[my changes]////////

        async private void RegisterBackgroundTask(object sender, RoutedEventArgs e)
        {
            try
            {
    
                // 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();  ////// [my changes] ////////
    
                // 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 = SampleBackgroundTaskName;
                geofenceTaskBuilder.TaskEntryPoint = SampleBackgroundTaskEntryPoint;
    
                // 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
                geofenceTask = geofenceTaskBuilder.Register();
    
                // Associate an event handler with the new background task
                geofenceTask.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
    
                UpdateButtonStates(/*registered:*/ true);
    
                ////// [my changes] ////////
                //switch (backgroundAccessStatus)
                //{
                //    case BackgroundAccessStatus.Unspecified:
                //    case BackgroundAccessStatus.Denied:
                //        rootPage.NotifyUser("This application must be added to the lock screen before the background task will run.", NotifyType.ErrorMessage);
                //        break;
    
                //    default:
                //        // Ensure we have presented the location consent prompt (by asynchronously getting the current
                //        // position). This must be done here because the background task cannot display UI.
                //        GetGeopositionAsync();
                //        break;
                //}
                ////// [my changes] ////////
            }
            catch (Exception ex)
            {
                // HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) == 0x80070032
                const int RequestNotSupportedHResult = unchecked((int)0x80070032);
    
                if (ex.HResult == RequestNotSupportedHResult)
                {
                    rootPage.NotifyUser("Location Simulator not supported.  Could not get permission to add application to the lock screen, this application must be added to the lock screen before the background task will run.", NotifyType.StatusMessage);
                }
                else
                {
                    rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage);
                }
    
                UpdateButtonStates(/*registered:*/ false);
            }
        }
    
    字符串出现异常:
    geofenceTask=geofenceTaskBuilder.Register()

    有人能帮我吗

    注意:msdn上的相同问题线程-

    这是一个已知的“错误”,请参阅。除此之外,我还没有得到答案

    您的问题已提交给相应的VS开发团队进行调查


    请投票

    您还有另一个问题,我认为您只需要注释几个字符串(////[my changes]////在我的代码示例中)BackgroundAccessStatus BackgroundAccessStatus=Wait BackgroundExecutionManager.RequestAccessAsync();-它将需要在真实的设备上,而不是模拟器上。这个复制步骤?“下载msdn()上的地理位置示例。打开Visual Studio 2013 RC并在定位计算机上调试应用程序以启用地理围栏后台任务(场景5)。停止调试。在模拟器上开始调试。将下拉列表更改为场景5(后台地理围栏),并在OnNavigatedTo方法中获取错误。”我读到,您没有提到BackgroundAccessStatus BackgroundAccessStatus=Wait BackgroundExecutionManager.RequestAccessAsync();这就是为什么,我认为你没有删除它的问题是相同的。位置后台代理在模拟器中失败