2016-05-14 102 views
0

我正在构建简单的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() 

我有什么图目前为止:

  1. 异常代码为80004002(E_NOINTERFACE)
  2. 我已经调查这是什么接口和发现,它在C:\Program Files (x86)\Windows Phone Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd宣布其居然有 ildasm window

    而且从Visual Studio项目的引用 solution references reference properties

  3. 每个其它触发器(SystemTrigger,MaintenanceTrigger等)被实例化细

我已经尝试过:

  1. 重新安装VS
  2. 清洁/重建解决方案
  3. 诠释方法RegisterBackgroundTask[STAThread]

这是我在Windows Phone和C#第一个应用,所以我可以在平常的地方做一些愚蠢的错误。我也不明白Visual Studio如何处理解决方案中的这些引用,以及在引用的.winmd文件中编码的接口如何变得可用于项目中的代码。也许有什么地方出了问题。所以我需要帮助寻找问题的根源并寻找解决方案。

谢谢您提前

+0

你[已要求(http://stackoverflow.com/questions/37043612/new-locationtriggerlocationtriggertype-geofence-fails-with-invalidcastexceptio)这个问题。接下来请致电Microsoft支持部门。 –

+0

现在我以不同角度提供不同的信息..也许知道它似乎更常见和可以理解的人expirienced – mshipov

回答

0

这可能是“LocationTrigger”是一个单身? (对不起,不知道电话),并且已经(已经)在其他地方被一个通用的System.__ ComObject RCW激活。如果是这种情况,则不能投射。请尝试使用Activator.CreateInstance。

Type t = Type.GetTypeFromProgID("WhateverTheProgIdIsHere"); 
System.Object obj = Activator.CreateInstance(t); 
ILocationTriggerFactory tf = obj as ILocationTriggerFactory; 
+0

谢谢你的答案。电话是HTC 8x。我会尽可能尝试这个解决方案 – mshipov

相关问题