Quickstart: Handling geofence notifications in the foreground (XAML)

Applies to Windows and Windows Phone

This topic will guide you through the steps of handling Geofence notification in the foreground, in your Windows Runtime app using C++, C#, or Visual Basic.

Roadmap: How does this topic relate to others? See:

Introduction

Once your geofences have been created, you will have to add the logic to handle what happens when a geofence event occurs. Depending on the MonitoredStates that you have set up, you may receive an event when:

  • The user has entered a region of interest.
  • The user has left a region of interest.
  • The geofence has expired or been removed. Note that a background app is not activated for a removal event.

You can listen for events directly from your app when it is running or register for a background task so that you receive a background notification when an event occurs. For more info about background task and geofences, see Quickstart: Listening for geofence events in the background, Quickstart: Handling geofence notifications from a background task and Guidelines for geofencing.

Register for geofence state change events

In order for your app to receive a foreground notification of a geofence state change, you must register an event handler. This is typically set up when you create the geofence.


private void Initialize()
{
    // other initialization logic

    GeofenceMonitor.Current.GeofenceStateChanged += OnGeofenceStateChanged;
}



Implement the geofence event handler

The next step is to implement the event handlers. The action taken here will depend on what your app is using the geofence for.


public async void OnGeofenceStateChanged(GeofenceMonitor sender, object e)
{
    var reports = sender.ReadReports();

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        foreach (GeofenceStateChangeReport report in reports)
        {
            GeofenceState state = report.NewState;

            Geofence geofence = report.Geofence;

            if (state == GeofenceState.Removed)
            {
                // remove the geofence from the geofences collection
                GeofenceMonitor.Current.Geofences.Remove(geofence);
            }
            else if (state == GeofenceState.Entered)
            {
                // Your app takes action based on the entered event

                // NOTE: You might want to write your app to take particular
                // action based on whether the app has internet connectivity.

            }
            else if (state == GeofenceState.Exited)
            {
                // Your app takes action based on the exited event

                // NOTE: You might want to write your app to take particular
                // action based on whether the app has internet connectivity.

            }
        }
    });
}





Related topics

Roadmaps
Roadmap for Windows Runtime apps using C# and Visual Basic
Roadmap for Windows Runtime apps using C++
Designing UX for apps
Tasks
Quickstart: Setting up a geofence
Quickstart: Listening for geofence events in the background
Quickstart: Handling geofence notifications from a background task
Reference
Geoshape
Geofence
Geolocator
Other resources
Guidelines for geofencing

 

 

Show:
© 2014 Microsoft