问题
I build simple app with usage of geofence api and background task to handle geofence changes. Here is part of code, wich try register background task
private async void RegisterBackgroundTask()
{
const string name = "GeofenceBackgroundTask";
if (BackgroundTaskRegistration.AllTasks.Any(task => task.Value.Name == name))
{
return;
}
var loc = await new Geolocator().GetGeopositionAsync(
TimeSpan.FromMinutes(2),
TimeSpan.FromSeconds(5)); //needed to trig user acceptance
var backgroundAccessStatus =
await BackgroundExecutionManager.RequestAccessAsync();
if (backgroundAccessStatus != BackgroundAccessStatus.Denied)
{
var geofenceTaskBuilder = new BackgroundTaskBuilder()
{
Name = name,
TaskEntryPoint = "RingtoneManager.Background.GeofenceBackgroundTask"
};
geofenceTaskBuilder.SetTrigger(new LocationTrigger(LocationTriggerType.Geofence));
geofenceTaskBuilder.Register();
}
}
And this line is always fail
new LocationTrigger(LocationTriggerType.Geofence)
with InvalidCastException
System.InvalidCastException was unhandled by user code
HResult=-2147467262
Message=Unable to cast object of type 'System.__ComObject' to type 'Windows.ApplicationModel.Background.ILocationTriggerFactory'.
Source=mscorlib
StackTrace:
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget)
at Windows.ApplicationModel.Background.LocationTrigger..ctor(LocationTriggerType triggerType)
at RingtoneManager3.App.<RegisterBackgroundTask>d__2.MoveNext()
InnerException:
I'll try register over trigger, such a system time trigger and there were no exceptions.
What i am doing wrong?
回答1:
I think you need to create geocircle first.
public static void CreateGeofence(BasicGeoposition position, double radius, string id = "default")
{
// The Geofence is a circular area centered at (latitude, longitude) point, with the
// radius in meter.
var geocircle = new Geocircle(position, radius);
// Sets the events that we want to handle: in this case, the entrace and the exit
// from an area of intereset.
var mask = MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited;
// Specifies for how much time the user must have entered/exited the area before
// receiving the notification.
var dwellTime = TimeSpan.FromSeconds(1);
// Creates the Geofence and adds it to the GeofenceMonitor.
var geofence = new Geofence(id, geocircle, mask, false, dwellTime);
try
{
GeofenceMonitor.Current.Geofences.Add(geofence);
}
catch (Exception e)
{
//Debug.WriteLine(e);
// geofence already added to system
}
}
and then create geofence for each location
CreateGeofence(new BasicGeoposition() { Longitude = double.Parse(VersionObject.store.lng), Latitude = double.Parse(VersionObject.store.lat) }, 200);
try
{
var backgroundStatus = await BackgroundExecutionManager.RequestAccessAsync();
var geofenceBuilder = new BackgroundTaskBuilder
{
Name = "Test Geofence",
TaskEntryPoint = "GeofencingTask.BackgroundGeofencing"
};
var trigger = new LocationTrigger(LocationTriggerType.Geofence);
geofenceBuilder.SetTrigger(trigger);
var geofenceTask = geofenceBuilder.Register();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
来源:https://stackoverflow.com/questions/37043612/new-locationtriggerlocationtriggertype-geofence-fails-with-invalidcastexceptio