从cocos2d-x wp8-xaml中的c++代码中调用c#delegate
Call c# delegate from c++ code in cocos2d-x wp8-xaml
我想在我的cocos2d-x3.3游戏(wp8-xaml后端)中从c++代码调用c#delegate。我发现:http://discuss.cocos2d-x.org/t/wp8-cocos2dx-and-xaml/4886/6
这是我在c++项目中的类"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)类中的回调:
public void CallNativeFunction()
{
Dispatcher.BeginInvoke(() =>
{
Debug.WriteLine("# NATIVE CODE #");
});
return;
}
这里有一个问题。在构造函数中,我必须创建新的NativeEventHelper(来自c++类),但我不知道如何添加引用,因为编译器抱怨未知标识符"NativeEventHelp"。
NativeEventHelper helper = new NativeEventHelper();
helper.SetCallNativeFunctionDelegate(CallNativeFunction);
我还发现:从WP8 中的C++代码调用C#方法
这似乎完全一样,但我不知道如何引用这个类。这在我的情况下不起作用: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
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 什么时候调用析构函数
- 如何用参数值调用函数(仅在运行时已知)
- 从cocos2d-x wp8-xaml中的c++代码中调用c#delegate
- 我想在*delegate中调用method.m从c++