Changing namespace name of C++ component in Windows Phone causes exception - windows-phone-8

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!

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.

Related

How to import into a C++/WinRT project a UserControl from a windows runtime component written in C#?

I have a very simple windows runtime component project written in C# in my solution where I added a UserControl that I would like to export to a C++/WinRT project.
This component works fine when imported into another C# project, but I get a compilation error when I try to import and use it in my C++/WinRT project.
\my_project\generated files\xamltypeinfo.g.cpp(1313): error C3083: 'my_component_XamlTypeInfo': the symbol to the left of a '::' must be a type
I added the reference to the component and I have added the following include to my pch.h thinking that it would resolve it:
#include "winrt/my_component.h"
but then I get the following compilation error:
\my_project\generated files\xamltypeinfo.g.cpp(1306): error C3083: 'my_component_XamlTypeInfo': the symbol to the left of a '::' must be a type
I noticed that there is also that if I include the header:
#include "winrt/data_grid.data_grid_XamlTypeInfo.h"
I no longer get a compiler error but the app crashes at startup when it tries to instantiate the component.
What is the normal procedure for importing windows runtime components that include XAML into C++/WinRT apps?

Base class variable is not accessible

I have two classes in Actionscript 3. I am using FlashBuilder 4.6. The SDK is 3.6A. The two classes are in a separate library. This library is referenced in the Active project.
My first (base) class is:
public class BaseDTO
{
public var errorCode:int;
public var errorMessage:String;
public function BaseDTO()
{
}
}
The second (derived) class is:
public class Configurations extends BaseDTO
{
}
In my active project (non-library), I am calling Configurations like this:
var c:Configurations = new Configurations();
c.errorCode = 0;
there are two references in two separate classes. Now the problems is that in first class which is basically a creationComplete handler of the application, I am getting a compile time error:
1119: Access of possibly undefined property errorCode through a
reference with static type dto.configs:Configurations.
And the other class which is calling the same code is throwing a runtime exception:
ReferenceError: Error #1056: Cannot create property errorCode on
dto.configs.Configurations.
I am not sure if I have explained it enough. Let me know if there are any other questions. I have been banging my head now for couple of hours now.
I have tried to create a new project, tried to use the same code to reference the configuration, and it works. Extremely strange.
Any Idea?
The Class was declared twice, in library and the project. Also, the namespace was same. Flex compiler should have raised warning on it though.

Windows RunTime Component with NetworkCredential in windows 8

I have created sample WindowsRunTimeComponent app in windows 8, with one property my class looks like..
namespace WindowsRunTimeComponentTest
{
public sealed class Class1
{
public NetworkCredential Credentials {get; set;}
}
}
when I tried to build this, its giving error :
Method 'WindowsRunTimeComponentTest.class1.Credentials.get()' returns System.Net.Credentials', which is not a valid Windows Runtime type. Methods exposed to Windows Runtime must return only Windows Runtime types.
I have vs2012.
please any idea what I have to change to resolve this issue?
My best guess is, when you create a Windows Runtime Component, your component can be used by languages that are not managed, like Javascript or C++.
Those languages don't know how to create specific .NET types such as NetworkCredentials.
For more info see the MSDN documentation here and this stackoverflow post.

WINRT XAML System.Void Compile Error in

after a little code change inside a Store App I ran into a compilation error:
-> System.Void cannot be used from C# -- use typeof(void) to get the void type object.
Problem is: this comes from a generated file: XamlTypeInfo.g.cs.
case "System.Void"
userType= new ... ,typeof(global::System.Void), ...
...
Rolling back the changes did not help, as deleting bin & obj, restarting, etc.
Is the actual System.Void case entry maybe an indicator that something within a XAML file could not be recognized by the code generator? Is there an System.Void entry in a working XamlTypeInfo.g.cs?
--- ADDITION ---
I can now produce the compile error when changing specific lines. I have a custom control deriving from ItemsControl. I define a regular DP which works fine. I am also providing AttachedProperties for Template, TemplateSelector and Style. Think of a Textbox that gets an Label via an AttachedProperty and its not just a string but like HeaderedControls you can define a Template etc. for the Lable.
The Problem is related to the Get/Set Methods for the AttachedProp. When I either change the Getter return type to DataTemplate or I comment out the Setter fully then the compile error comes:
public static DataTemplate GetLabelTEmplate(UIElement element)
{
return (DataTemplate)element.GetValue(LabelTemplateProperty;
}
public static void SetLabelTemplate(UIElement element, object value)
{
element.SetValue(LabelTemplateProperty, value);
}
Any ideas would be highly appreciated.
Best regards
Gope
After filing a bug complaint with microsoft they pointed me to the problem: The setter's value cannot be of type object. This information is needed for the XamlTypeInfo generation so when I changed object to DataTemplate it compiled.
Although I haven't tried it yet, I believe object is fine for plain WPF, but for Win 8 Store apps this does result in an compilation Error. Funny stuff... :)

WP8: can't consume a native component

Windows Phone 8 C# project (MyApp), migrated from WP7.1. I've added a native Windows Runtime component library (AppLib) to the solution, created a reference. There's a public sealed ref class (MyClass) in it. There's a reference to it in the C# code (in OnLoaded of the main XAML page). The whole thing compiles - meaning the metadata of the component is being generated.
When I'm trying to run, the project fails with the exception or type TypeLoadException with the following message:
Typename or Namespace was not found in metadata file. (Exception from HRESULT: 0x8000000F)
Both AppLib.DLL and AppLib.winmd can be found in the XAP. The winmd contains the information about the type, and in the right namespace, too. What else should I check?
At exception time, the AppLib.dll is not listed in the modules window of the debugger. It's as if the DLL loading fails for some reason.
I've tried with brand new class in an arbitrary namespace - same problem. Looks like the problem is on the DLL level, not on class level.
The name of the WinMD file must be a prefix of the name of the namespace in which any public and activatable types are declared. For example, if your WinMD is named AppLib.winmd, your MyClass type must be defined in namespace AppLib or some other namespace nested within that namespace, for example AppLib::Something.
It must also be declared in the "best matching" WinMD, so if your type is named A.B.MyClass and you have both A.winmd and A.B.winmd in your package, the type must be defined in A.B.winmd.
The Windows Runtime uses the name of the type to determine which WinMD file defines the type. See also my answer to "XAML cannot find reference in local namespace."