我正在構建簡單的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