从 cocos2d-x wp8-xaml 中的 c++ 代码调用 c# 委托
Call c# delegate from c++ code in cocos2d-x wp8-xaml
我想在我的 cocos2d-x 3.3 游戏(wp8-xaml 后端)中从 c++ 代码调用 c# 委托。
我找到了这个:
http://discuss.cocos2d-x.org/t/wp8-cocos2dx-and-xaml/4886/6
这是我在 C++ 项目中的 class "NativeEventHelper.cpp":
#pragma once
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
namespace PhoneDirect3DXamlAppComponent
{
public delegate void CallNativeFunctionDelegate();
public ref class NativeEventHelper sealed
{
public:
NativeEventHelper(void);
void SetCallNativeFunctionDelegate(CallNativeFunctionDelegate^ delegate) {
m_CallNativeFunctionDelegate = delegate;
}
bool NativeEventHelper::CallNativeFunction()
{
if (m_CallNativeFunctionDelegate)
{
m_CallNativeFunctionDelegate->Invoke();
return true;
}
return false;
}
private:
property static CallNativeFunctionDelegate^ m_CallNativeFunctionDelegate;
};
}
#endif
这是我在 C# 中的回调 (MainPage.xaml.cs) class:
public void CallNativeFunction()
{
Dispatcher.BeginInvoke(() =>
{
Debug.WriteLine("# NATIVE CODE #");
});
return;
}
这里有一个问题。在构造函数中,我必须创建新的 NativeEventHelper(来自 c++ class),但我不知道如何添加引用,因为编译器抱怨未知标识符 "NativeEventHelper".
NativeEventHelper helper = new NativeEventHelper();
helper.SetCallNativeFunctionDelegate(CallNativeFunction);
我还发现了这个:
Calling C# method from C++ code in WP8
这似乎完全一样,但我又不知道如何引用这个class。这在我的情况下不起作用:https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications 而不是 windows 我在参考中看到 windows phone sdk 并且无法添加 winrt。
我终于解决了!!
首先:我必须将命名空间更改为 cocos2d。此外,我不得不忽略警告并完全清理并重建。之后就可以了。要在 C++ 中调用代码,我想出了这个:
NativeEventHelper^ nativeEventHelper = ref new NativeEventHelper();
nativeEventHelper->CallNativeFunction();
已修复 NativeEventHelper.cpp 文件:
#pragma once
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
namespace cocos2d
{
public delegate void CallNativeFunctionDelegate();
public ref class NativeEventHelper sealed
{
public:
NativeEventHelper(void);
void SetCallNativeFunctionDelegate(CallNativeFunctionDelegate^ delegate) {
m_CallNativeFunctionDelegate = delegate;
}
bool NativeEventHelper::CallNativeFunction()
{
if (m_CallNativeFunctionDelegate)
{
m_CallNativeFunctionDelegate->Invoke();
return true;
}
return false;
}
private:
property static CallNativeFunctionDelegate^ m_CallNativeFunctionDelegate;
};
}
#endif
我想在我的 cocos2d-x 3.3 游戏(wp8-xaml 后端)中从 c++ 代码调用 c# 委托。 我找到了这个: http://discuss.cocos2d-x.org/t/wp8-cocos2dx-and-xaml/4886/6
这是我在 C++ 项目中的 class "NativeEventHelper.cpp":
#pragma once
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
namespace PhoneDirect3DXamlAppComponent
{
public delegate void CallNativeFunctionDelegate();
public ref class NativeEventHelper sealed
{
public:
NativeEventHelper(void);
void SetCallNativeFunctionDelegate(CallNativeFunctionDelegate^ delegate) {
m_CallNativeFunctionDelegate = delegate;
}
bool NativeEventHelper::CallNativeFunction()
{
if (m_CallNativeFunctionDelegate)
{
m_CallNativeFunctionDelegate->Invoke();
return true;
}
return false;
}
private:
property static CallNativeFunctionDelegate^ m_CallNativeFunctionDelegate;
};
}
#endif
这是我在 C# 中的回调 (MainPage.xaml.cs) class:
public void CallNativeFunction()
{
Dispatcher.BeginInvoke(() =>
{
Debug.WriteLine("# NATIVE CODE #");
});
return;
}
这里有一个问题。在构造函数中,我必须创建新的 NativeEventHelper(来自 c++ class),但我不知道如何添加引用,因为编译器抱怨未知标识符 "NativeEventHelper".
NativeEventHelper helper = new NativeEventHelper();
helper.SetCallNativeFunctionDelegate(CallNativeFunction);
我还发现了这个: Calling C# method from C++ code in WP8
这似乎完全一样,但我又不知道如何引用这个class。这在我的情况下不起作用:https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications 而不是 windows 我在参考中看到 windows phone sdk 并且无法添加 winrt。
我终于解决了!!
首先:我必须将命名空间更改为 cocos2d。此外,我不得不忽略警告并完全清理并重建。之后就可以了。要在 C++ 中调用代码,我想出了这个:
NativeEventHelper^ nativeEventHelper = ref new NativeEventHelper();
nativeEventHelper->CallNativeFunction();
已修复 NativeEventHelper.cpp 文件:
#pragma once
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
namespace cocos2d
{
public delegate void CallNativeFunctionDelegate();
public ref class NativeEventHelper sealed
{
public:
NativeEventHelper(void);
void SetCallNativeFunctionDelegate(CallNativeFunctionDelegate^ delegate) {
m_CallNativeFunctionDelegate = delegate;
}
bool NativeEventHelper::CallNativeFunction()
{
if (m_CallNativeFunctionDelegate)
{
m_CallNativeFunctionDelegate->Invoke();
return true;
}
return false;
}
private:
property static CallNativeFunctionDelegate^ m_CallNativeFunctionDelegate;
};
}
#endif