2016-05-14 105 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

相關問題