无法转换类型为#System; .__ ComObject'的对象。键入' Windows.ApplicationModel.Background.ILocationTriggerFactory

时间:2016-05-14 09:07:50

标签: c# com windows-runtime windows-phone-8.1 winmd

我建立简单的win phone 8.1 app,当用户进入/离开某个区域时,使用geofence api和后台任务进行控制。
要注册后台任务,我在RegisterBackgroundTask

中实现App方法
    /// <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 e.t.c)正在实例化
  4. 我已经尝试过:

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

      这是我在Windows手机和c#上的第一个应用程序,所以我可能会在常见的地方做一些愚蠢的错误。我也不明白visual studio如何处理这些来自解决方案的引用,以及如何在引用的.winmd文件中编码的接口可用于项目中的代码。也许哪里出了问题。所以我需要帮助搜索问题的根源并找到解决方案。

      提前谢谢

1 个答案:

答案 0 :(得分:0)

可能是“LocationTrigger”是单身? (抱歉,不知道电话)并且已经(已经)使用通用系统.__ ComObject RCW在其他地方激活。如果是这种情况则无法投射。请尝试使用Activator.CreateInstance。

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