[英]Make calls between C++ and C# code
我端起我的游戏到Windows Phone 8,通过了cocos2d-x的C ++项目,称为MyGameComponent,这是所谓的MyGame C#项目的依赖关系创建的Visual Studio项目。 在MyGame中 ,cocos2d-x为您提供LocalizedStrings.cs
文件。 我希望能够调用该文件,以便可以使用本机本地化系统。 我怎样才能做到这一点?
我也想将此功能用于其他功能,因此请不要提出另一种进行本地化的方法。
我正在使用cocos2d-x v2.2.5。
您可以将委托从C#传递给C ++组件。
在C ++组件中,您将定义一个委托和一个用于设置该委托的方法调用,如下所示:
public delegate Platform::String^ GetTranslatedStringDelegate();
public ref class Component sealed
{
public:
void SetDelegate(GetTranslatedStringDelegate^);
private:
Platform::Agile<GetTranslatedStringDelegate> m_delegate;
}
然后,在C#中,您可以创建一个与委托签名匹配的函数,并将其传递给您的组件:
public string GetTranslatedString()
{
return "translated string";
}
// elsewhere in your C# code
component.SetDelegate(GetTranslatedString);
然后在C ++中,只要需要获取翻译后的字符串,就可以调用委托方法。
编辑:我写了一篇有关此主题的博客文章-http: //robwirving.com/2014/07/21/calling-c-methods-c-winrt-components/
如果您使用的是托管c ++,则只需在C ++项目中添加C#dll作为引用即可。 然后只需正常调用C#Dll。
ManagedCSharp::ManagedClass::ShowValue();
其中ManagedCSharp是命名空间,ManagedClass是Class,ShowValue是方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.