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?