staticfloat
staticfloat

Reputation: 7040

Changing namespace name of C++ component in Windows Phone causes exception

I have a C++ runtime component in a WP8 application, and if I change the namespace name, I get a "TargetInvocation" exception thrown whenever I try to instantiate a class in that namespace.

As an example, if I create the default C++ Windows Runtime Component, the header looks like this:

#pragma once

namespace CppComponent1
{
    public ref class WindowsPhoneRuntimeComponent sealed
    {
    public:
        WindowsPhoneRuntimeComponent();
    };
}

If I change CppComponent1 to CppComponent2 in the .h and the .cpp, and then try to instantiate a WindowsPhoneRuntimeComponent object in my C# code, I get the following error:

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

How can I change the namespace of a native module in a WP8 app? Thanks!

Upvotes: 3

Views: 1939

Answers (1)

James McNellis
James McNellis

Reputation: 355079

The name of the Windows Metadata (WinMD) file that declares the component must be a prefix of the namespace in which the public types are declared. (I provided a slightly more detailed explanation of the namespace rules in an answer to another question.)

If you rename the namespace from CppComponent1 to CppComponent2, you also need to rename the WinMD file produced by the build from CppComponent1.winmd to CppComponent2.winmd.

Upvotes: 6

Related Questions