Geofence Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Contains the functionality to define a geofence (a geographical area of interest) to monitor.
public ref class Geofence sealed
/// [Windows.Foundation.Metadata.Activatable(Windows.Devices.Geolocation.Geofencing.IGeofenceFactory, 65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class Geofence final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Activatable(Windows.Devices.Geolocation.Geofencing.IGeofenceFactory, 65536, "Windows.Foundation.UniversalApiContract")]
class Geofence final
[Windows.Foundation.Metadata.Activatable(typeof(Windows.Devices.Geolocation.Geofencing.IGeofenceFactory), 65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class Geofence
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Activatable(typeof(Windows.Devices.Geolocation.Geofencing.IGeofenceFactory), 65536, "Windows.Foundation.UniversalApiContract")]
public sealed class Geofence
function Geofence(id, geoshape, monitoredStates, singleUse, dwellTime, startTime, duration)
Public NotInheritable Class Geofence
- Inheritance
- Attributes
Windows requirements
| Requirements | Description |
|---|---|
| Device family |
Windows 10 (introduced in 10.0.10240.0)
|
| API contract |
Windows.Foundation.UniversalApiContract (introduced in v1.0)
|
| App capabilities |
location
|
Examples
This example demonstrates creating and registering basic geofences:
using Windows.Devices.Geolocation;
using Windows.Devices.Geolocation.Geofencing;
private Geofence CreateGeofence(string id, double latitude, double longitude, double radiusMeters)
{
var position = new BasicGeoposition
{
Latitude = latitude,
Longitude = longitude
};
var geocircle = new Geocircle(position, radiusMeters);
var monitoredStates = GeofenceState.Entered | GeofenceState.Exited;
return new Geofence(id, geocircle, monitoredStates, false);
}
private async Task<bool> RegisterGeofenceAsync(Geofence geofence)
{
var accessStatus = await Geolocator.RequestAccessAsync();
if (accessStatus != GeolocationAccessStatus.Allowed)
{
return false;
}
try
{
var monitor = GeofenceMonitor.Current;
monitor.Geofences.Add(geofence);
return true;
}
catch (Exception)
{
// App-specific: handle registration failure
return false;
}
}
For comprehensive examples including validation, lifecycle management, background monitoring, and advanced patterns, see the Geolocation sample.
Remarks
Creating geofences
Geofences define geographic areas to monitor for device entry and exit events. Each geofence requires:
- Unique identifier: Used to distinguish between multiple geofences
- Geographic shape: Currently supports circular areas (Geocircle)
- Monitoring states: Combination of GeofenceState values (Entered, Exited, Removed)
Key properties
- Id: Unique identifier for the geofence (up to 64 characters)
- Geoshape: Geographic boundary definition
- MonitoredStates: Which state changes to track
- StartTime: When monitoring should begin
- Duration: How long to monitor (0 for indefinite)
- DwellTime: Minimum time to remain in area before triggering
- SingleUse: Whether to remove after first trigger
Lifecycle management
Once created, geofences are registered with GeofenceMonitor.Geofences and persist until:
- Manually removed from the collection
- Automatically removed due to expiration (Duration elapsed)
- Automatically removed after single use (if SingleUse is true)
Important
Geofences with duplicate Id values cannot be registered simultaneously. Remove existing geofences before adding replacements with the same identifier.
Note
The system monitors registered geofences continuously, even when the app is suspended. Use background tasks to handle geofence events when the app is not running.
Note
During remote desktop sessions, geofence events may use the remote session's reported location if a geolocation provider override is active. Manage overrides with the GeolocationProvider API.
Constructors
| Name | Description |
|---|---|
| Geofence(String, IGeoshape, MonitoredGeofenceStates, Boolean, TimeSpan, DateTime, TimeSpan) |
Initializes a new Geofence object given the id, the shape of the geofence, the states to monitor the geofence for, the singleUse flag, the dwellTime for the geofence, the time to start monitoring the geofence, and the duration of the geofence. |
| Geofence(String, IGeoshape, MonitoredGeofenceStates, Boolean, TimeSpan) |
Initializes a new Geofence object given the id, the shape of the geofence, the states to monitor the geofence for, the singleUse flag, and the dwellTime for the geofence. |
| Geofence(String, IGeoshape, MonitoredGeofenceStates, Boolean) |
Initializes a new Geofence object given the id, the shape of the geofence, the states to monitor the geofence for, and the singleUse flag. |
| Geofence(String, IGeoshape) |
Initializes a new Geofence object given the id and the shape of the geofence. |
Properties
| Name | Description |
|---|---|
| Duration |
Gets the time window, beginning after the StartTime, during which the Geofence is monitored. |
| DwellTime |
The minimum time that a position has to be inside or outside of the Geofence in order for the notification to be triggered. |
| Geoshape |
The shape of the geofence region. |
| Id |
The id of the Geofence. |
| MonitoredStates |
Indicates the states that the Geofence is being monitored for. |
| SingleUse |
Indicates whether the Geofence should be triggered once or multiple times. |
| StartTime |
The time to start monitoring the Geofence. |