【问题标题】:Unable to cast object of type 'System.__ComObject' to type 'Windows.ApplicationModel.Background.ILocationTriggerFactory无法将“System.__ComObject”类型的对象转换为“Windows.ApplicationModel.Background.ILocationTriggerFactory”
【发布时间】:2016-05-14 09:07:50
【问题描述】:

我正在构建简单的 win phone 8.1 应用程序,当用户进入/离开某个区域时,它使用地理围栏 API 和后台任务进行控制。
要注册后台任务,我在App 类中实现RegisterBackgroundTask 方法

    /// <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 中声明,并且它实际上在那里

    它引用自 Visual Studio 项目

  3. 其他所有触发器(SystemTrigger、MaintenanceTrigger 等)都可以正常实例化

我已经尝试过的:

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

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

提前谢谢你

【问题讨论】:

  • already asked这个问题。是时候致电 Microsoft 支持了。
  • 现在我以不同的角度提供不同的信息。也许知道对于有经验的人来说这似乎更常见和更容易理解

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


【解决方案1】:

可能“LocationTrigger”是单例? (对不起,不知道电话)并且(已经)在其他地方使用通用 System.__ComObject RCW 激活。如果是这种情况,则无法强制转换。尝试改用 Activator.CreateInstance。

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

【讨论】:

  • 感谢您的回答。手机是 HTC 8x。我会尽可能尝试这个解决方案
猜你喜欢
  • 2016-02-02
  • 2018-06-05
  • 2018-06-20
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
相关资源
最近更新 更多