wp8 c++ call c# function with param


#1

anyone know , in cocos2dx-wp8 , c++ how to call c# function with param?
now i want to call webbrowser with URL, but now URL is in Cpp, and i don’t know how to transfer it to C#
if you can resolve it, please help me, thank you!


#2

i was also facing similar kind of problem… but i have somehow managed to get the solution n i would like to share it with everyone who is facing same problem…
I have referred following links:
http://stackoverflow.com/questions/17304386/how-does-one-make-function-calls-or-trigger-events-from-a-native-component-into
https://github.com/cocos2d/cocos-docs/blob/master/manual/framework/native/sdk-integration/wp8-webbrowser/zh.md

Following is the solution that is working in my code:

In HelloWorldScene.h file insert the following Code:-

if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)

namespace PhoneDirect3DXamlAppComponent
{

[Windows::Foundation::Metadata::WebHostHidden]
public interface class ICallback
{
public:

//Exec(…) is the virtual method that will be implemented in your .cs file
virtual void Exec( Platform::String ^Command, Platform::String ^Param);
};

public ref class BroswerEventHelper sealed
{
public:
BroswerEventHelper ( void );
static void SetCallback( ICallback ^Callback); //
static void callCSharp();
};

}

endif

Now in your HelloWorldScene.cpp file insert the following code

using namespace PhoneDirect3DXamlAppComponent;

if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)

namespace PhoneDirect3DXamlAppComponent {
BroswerEventHelper::BroswerEventHelper(void){

}

ICallback ^CSCallback = nullptr;
void BroswerEventHelper::SetCallback( ICallback ^Callback)
{
HelloWorld::showText();
CSCallback = Callback;
}

void BroswerEventHelper::callCSharp(){
CCLog(“in CPP method”);
if (CSCallback != nullptr){
CCLog(“Value not null”);
//Send your parameters from here
CSCallback->Exec( “Hello”, “Suveer Jacob” );
}
}
}

endif

** to call the method “Exec” just use the following code at any event or button click:-
BroswerEventHelper::callCSharp();

Now in your “MainPage.xaml.cs” file insert the following code:-

public class CallbackImpl : ICallback
{

public CallbackImpl() {
Debug.WriteLine(“in c-sharp constructor”);
BroswerEventHelper.SetCallback(this);
}
public void Exec(String Command, String Param)
{
//Execute some C# code, if you call UI stuff you will need to call this too
/Deployment.Current.Dispatcher.BeginInvoke(() =>
{
});
/
Debug.WriteLine(“C# method—”+Command+"-----"+Param);

}
}
now in the same file(ie “MainPage.xaml.cs”) insert the following code in ‘DrawingSurface_Loaded(object sender, RoutedEventArgs e)’ method

CallbackImpl CI = new CallbackImpl();

** you will encounter some problems like " ‘BroswerEventHelper’ class undefined…" or any thing like that, so don’t panic just clean n rebuild your project n HOPEFULLY your project will compile successfully. ***

Best Regards
Suveer Jacob


#3

Good job, suveerjacob, and there is another tutorial: https://github.com/cocos2d/cocos-docs/blob/master/manual/framework/native/sdk-integration/wp8-thirdSDK/zh.md, it’s about how to call thirdSDK.


#4

Thanx Chenjc, I have noticed some unusual things in the current cocos2dx version 2.2.2 or the cocos2d-x-master(from github). For Example: ‘sprite->stopActionByTag(1)’ is not working n ‘kResolutionNoBorder’ policy is also not working… I am facing so much difficulty in porting my game in wp8, as for android n iphone i used cocos2d-x-2.0.4 version.
Please let me know if there is any other alternative for it.


#5

Hi suveerjacob, stopActionByTag can work well,you can check the testcpp and find it working well in SpriteEase::testStopAction or any other place. If you faced with any other problem, share in the forum and we will check it soon. Thanks.


#6

Is it working in cocos2d-x v3? (I use 3.3). I cannot get this working. I’ve created the same code as you, it’s compiling fine, but just after splashscreen and I’m getting following error:

Unhandled exception at 0x76F5210B (KERNELBASE.DLL) in TaskHost.exe: 0xE0434352 (parameters: 0x80131604, 0x00000000, 0x00000000, 0x00000000, 0x6D9C0000).

In this tutorial: http://www.cocos2d-x.org/wiki/How_to_integrate_third_SDK there’s one more line:

D3DComponent.SetCallback( CI);

However there’s no D3DComponent object in my project and I cannot find any other with SetCallback method.


#7

Help me please :smiley: ?