Search code examples
c#c++xamlwindows-phone-8

Call c# delegate from c++ code in cocos2d-x wp8-xaml


I want to call c# delegate from c++ code in my cocos2d-x 3.3 game (wp8-xaml backend). I found this: http://discuss.cocos2d-x.org/t/wp8-cocos2dx-and-xaml/4886/6

And here's my class "NativeEventHelper.cpp" in c++ project:

#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

Here's my callback in c# (MainPage.xaml.cs) class:

 public void CallNativeFunction()
    {
        Dispatcher.BeginInvoke(() =>
        {
            Debug.WriteLine("# NATIVE CODE #");
        });
        return;
    }

And here's an issue. In constructor I have to create new NativeEventHelper (from c++ class), but I don't know how to add refence, because compiler is complaining about unknown identifier "NativeEventHelper".

 NativeEventHelper helper = new NativeEventHelper();
 helper.SetCallNativeFunctionDelegate(CallNativeFunction);

I also found this: Calling C# method from C++ code in WP8

This seems to be exactly the same, but again I don't know how to reference this class. This is not working in my case: https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications Instead of windows I see windows phone sdk in references and cannot add winrt.


Solution

  • I finally solved it!!

    First of all: I had to change namespace to cocos2d. Also I had to ignore warnings and just make full clean and rebuild. After that it works. To call code in c++ I figured out this:

    NativeEventHelper^ nativeEventHelper = ref new NativeEventHelper();
    nativeEventHelper->CallNativeFunction();
    

    Fixed NativeEventHelper.cpp file:

    #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