在Windows Phone中更改C++组件的命名空间名称会导致异常

sta*_*oat 3 c++-cx windows-phone-8

我在WP8应用程序中有一个C++运行时组件,如果我更改命名空间名称,每当我尝试在该命名空间中实例化一个类时,我都会抛出一个"TargetInvocation"异常.

例如,如果我创建默认的C++ Windows运行时组件,标题如下所示:

#pragma once

namespace CppComponent1
{
    public ref class WindowsPhoneRuntimeComponent sealed
    {
    public:
        WindowsPhoneRuntimeComponent();
    };
}
Run Code Online (Sandbox Code Playgroud)

如果我更改CppComponent1CppComponent2.h和.cpp,然后尝试WindowsPhoneRuntimeComponent在我的C#代码中实例化一个对象,我会收到以下错误:

A first chance exception of type 'System.TypeLoadException' occurred in Unknown Module.
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in Microsoft.Phone.ni.dll
Run Code Online (Sandbox Code Playgroud)

如何在WP8应用程序中更改本机模块的命名空间?谢谢!

Jam*_*lis 6

声明组件的Windows元数据(WinMD)文件的名称必须是声明公共类型的命名空间的前缀.(在另一个问题的答案中,我提供了一个稍微更详细的命名空间规则说明.)

如果将命名空间重命名CppComponent1CppComponent2,则还需要将构建生成的WinMD文件重命名CppComponent1.winmdCppComponent2.winmd.

  • 大!您可以通过单击C++项目来更改`$(RootNamespace)`宏,点击ALT-ENTER打开"属性"窗口(右键单击项目并单击"属性",不是相同的"属性")并更改"Root Namespace"的值!您可能希望使用此信息更新您的答案,以便搜索此内容的其他人可以找到它. (2认同)