我正在构建简单的win phone 8.1应用程序,它使用geofence api和后台任务进行控制,当用户进入/离开某个区域时。
要注册后台任务我实现RegisterBackgroundTask
方法App
类无法投入'System .__ ComObject'类型的对象来键入'Windows.ApplicationModel.Background.ILocationTriggerFactory
/// <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();
}
}
这是一部分,这引发异常 new LocationTrigger(LocationTriggerType.Geofence)
异常详细信息:
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()
我有什么图目前为止:
- 异常代码为80004002(E_NOINTERFACE)
我已经调查这是什么接口和发现,它在
C:\Program Files (x86)\Windows Phone Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd
宣布其居然有而且从Visual Studio项目的引用
- 每个其它触发器(SystemTrigger,MaintenanceTrigger等)被实例化细
我已经尝试过:
- 重新安装VS
- 清洁/重建解决方案
- 诠释方法
RegisterBackgroundTask
与[STAThread]
这是我在Windows Phone和C#第一个应用,所以我可以在平常的地方做一些愚蠢的错误。我也不明白Visual Studio如何处理解决方案中的这些引用,以及在引用的.winmd文件中编码的接口如何变得可用于项目中的代码。也许有什么地方出了问题。所以我需要帮助寻找问题的根源并寻找解决方案。
谢谢您提前
你[已要求(http://stackoverflow.com/questions/37043612/new-locationtriggerlocationtriggertype-geofence-fails-with-invalidcastexceptio)这个问题。接下来请致电Microsoft支持部门。 –
现在我以不同角度提供不同的信息..也许知道它似乎更常见和可以理解的人expirienced – mshipov