BackgroundTaskBuilder Register() issue

懵懂的女人 提交于 2020-03-23 18:21:56

问题


I was developing application with Geofencing, when stuck at issue:

ArgumentException (Value does not fall within the expected range) when I am trying to register background task for geofencing.

Sample from MSDN has the same problem. I will show code from sample for clarity (link to sample: http://code.msdn.microsoft.com/windowsapps/Geolocation-2483de66#content use scenario 5, http://msdn.microsoft.com/en-us/library/windows/apps/dn440583.aspx - instruction how to test).

All what I have done:

  1. Build my app in Visual Studio.
  2. Deploy the app locally first and add the app to the lock screen in Settings.
  3. Close your app that is running locally.
  4. Launch your app in the Visual Studio simulator.
  5. Call RegisterBackgroundTask(..) (simply pressed button register in scenario 5)

There is code from MSDN sample that I have commented for successful app deploying in Simulator - tagged with ////// [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);
        }
    }

Exception falls on string: geofenceTask = geofenceTaskBuilder.Register();

Can anybody help me?

P.S. Same question thread on msdn - http://social.msdn.microsoft.com/Forums/en-US/3d69f2f9-93e0-401b-8a13-598dc671fa4f/backgroundtask-register?forum=winappswithcsharp


回答1:


This is a known "bug" see Windows Store 8.1 Location background tasks do not work in simulator. I have yet to get an answer other than

Your issue has been routed to the appropriate VS development team for investigation

Please upvote it!



来源:https://stackoverflow.com/questions/20053961/backgroundtaskbuilder-register-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!