I'm building simple win phone 8.1 app which using geofence api and background tasks for controlling, when user enter/leaves some area.
To register background task i implement RegisterBackgroundTask
method in App
class
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.Suspending += this.OnSuspending;
this.RegisterBackgroundTask();
}
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 is the part, which raise the exception
new LocationTrigger(LocationTriggerType.Geofence)
Exception details:
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()
What i have figure out so far:
- Exception code is 80004002 (E_NOINTERFACE)
I've investigate what is this interface and found, that it declared in
C:\Program Files (x86)\Windows Phone Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd
and its actually thereAnd its referenced from visual studio project
- Every other triggers (SystemTrigger, MaintenanceTrigger e.t.c) are instantiating fine
What i have already tried:
- Reinstall VS
- Clean/Rebuild solution
- Annotating method
RegisterBackgroundTask
with[STAThread]
This is my first app on windows phone and on c#, so i may do some stupid mistakes in common place. I'm also dont understand how visual studio process those references from solution and how interfaces, that coded in referenced .winmd files, became available to code in project. Maybe there is where something goes wrong. So i need help in searching the root of problem and finding the solution.
Thank you in advance