Problem
I try to call BackgroundTask when a Geofence (Enter / Exit) event occurs in WP8.1 (WinRT). I wrote an example application to try to get it to work, but it seems it can't do it.
So far, these have been the steps I took to try to get Geofences to work in the background:
- Check accommodation options
- Create + Register geo-photography
- Create + register a BackgroundTask that listens for
LocationTrigger(LocationTriggerType.Geofence); - In my background task, a simple popup notification is triggered
What I did to troubleshoot
I included in my app.manifest:
- Toast Capable => Yes
- Features: location, Internet (client & amp; Server)
- Classifieds: BackgroundTasks (Location). EntryPoint =
BackgroundTask.GeofenceBackgroundTask
My background task is in a separate project called BackgroundTask . This is a component of WindowsRT and contains one GeofenceBackgroundTask class.

Project example
The project code can be found on this [link] ( https://github.com/kiangtengl/GeofenceSample ):
How to test

Click Register Geofence + BackgroundTasks
Exit the application (press the home button)
Change your current location anywhere within 100 meters of the location you previously set. A notification should appear.
Project Code:
Check location options
public static async Task GetLocationCapabilities() { try { var geolocator = new Geolocator(); await geolocator.GetGeopositionAsync(); var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync(); Debug.WriteLine("background access status" + backgroundAccessStatus); } catch (UnauthorizedAccessException e) { Debug.WriteLine(e); } catch (TaskCanceledException e) { Debug.WriteLine(e); } }
Create Geofence
public static void CreateGeofence(BasicGeoposition position, double radius, string id = "default") {
Register background task
public static async Task RegisterBackgroundTask() { try { // Create a new background task builder var geofenceTaskBuilder = new BackgroundTaskBuilder() { Name = GeofenceBackgroundTaskName, TaskEntryPoint = "BackgroundTask.GeofenceBackgroundTask" }; // Create a new location trigger var trigger = new LocationTrigger(LocationTriggerType.Geofence); // Associate the location trigger with the background task builder geofenceTaskBuilder.SetTrigger(trigger); var geofenceTask = geofenceTaskBuilder.Register(); // Associate an event handler with the new background task geofenceTask.Completed += (sender, e) => { try { e.CheckResult(); } catch(Exception error) { Debug.WriteLine(error); } }; } catch(Exception e) { // Background task probably exists Debug.WriteLine(e); } }
BackgroundTask code to run the toast
namespace BackgroundTask { public sealed class GeofenceBackgroundTask : IBackgroundTask { public void Run(IBackgroundTaskInstance taskInstance) { var toastTemplate = ToastTemplateType.ToastText02; var toastXML = ToastNotificationManager.GetTemplateContent(toastTemplate); var textElements = toastXML.GetElementsByTagName("text"); textElements[0].AppendChild(toastXML.CreateTextNode("You have left!")); var toast = new ToastNotification(toastXML); ToastNotificationManager.CreateToastNotifier().Show(toast); } } }
source share