首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将“System.__ComObject”类型的对象强制转换为System.__ComObject类型

无法将“System.__ComObject”类型的对象强制转换为System.__ComObject类型
EN

Stack Overflow用户
提问于 2016-05-14 09:07:50
回答 1查看 895关注 0票数 0

我正在建立简单的win phone 8.1应用程序,它使用geofence api和后台任务进行控制,当用户进入/离开某个区域时。

为了注册后台任务,我在RegisterBackgroundTask类中实现了App方法

代码语言:javascript
复制
    /// <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)的部分

例外细节:

代码语言:javascript
复制
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项目。

  1. 所有其他触发器(SystemTrigger,MaintenanceTrigger e.t.c)都在实例化。

我已经尝试过的是:

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

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

提前谢谢你

EN

回答 1

Stack Overflow用户

发布于 2017-01-26 19:26:04

可能是"LocationTrigger“是独生子女吗?(对不起,不知道电话),并且(已经)已经用一个通用的System.__ComObject RCW激活了其他地方。如果是这样的话,就不能进行转换。尝试使用Activator.CreateInstance代替。

代码语言:javascript
复制
Type t = Type.GetTypeFromProgID("WhateverTheProgIdIsHere");
System.Object obj = Activator.CreateInstance(t);
ILocationTriggerFactory tf = obj as ILocationTriggerFactory;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37224786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档