I'm interested in calling a C# method from C++ code in Windows Phone 8. I have already learned how to pass a callback function to C++ code from C# via delegate declarations in my C++ code, but I am looking to see if I can do any of the following:
我有兴趣从Windows Phone 8中的c++代码调用c#方法。我已经学会了如何通过我的c++代码中的委托声明将一个回调函数传递给c#中的c++代码,但是我想看看我是否能够做到以下任何一种:
-
Call certain methods directly from the C++ code. This would involve somehow inspecting the C# object makeup from C++, and seems unlikely to me, but I thought I'd ask you all anyway
直接从c++代码调用某些方法。这将涉及到从c++中检查c#对象的组成,并且看起来不太可能,但是我想我还是会问你所有的问题。
-
Trigger events in the C# code, which can then be handled by C# methods
在c#代码中触发事件,然后c#方法可以处理这些事件
-
Use a dispatcher to call C# callbacks in the Main UI thread so that the callbacks can modify UI elements
使用分派器在主UI线程中调用c#回调,以便回调能够修改UI元素
-
Use a dispatcher to trigger events in the C# code, (Essentially a merging of the above two points)
使用一个dispatcher来触发c#代码中的事件(本质上是上述两点的合并)
In short, I am looking for as many C++ -->C# communication tips as you guys can throw me, I want to learn it all. :)
简而言之,我正在寻找尽可能多的c++——> c#沟通技巧,我想学习所有的知识。:)
2 个解决方案
#1
20
By getting an object in C# code to implement a Windows RT interface, and passing down a reference to this object, it is possible to do all of the above with a bit of set-up (if I understand correctly - not sure about exactly what you want to do with your Dispatcher examples - you might want to wrap the Dispatcher on the C# side).
通过一个对象在c#代码实现一个Windows RT接口,并对该对象的引用,可以做所有以上的设置(如果我理解正确的话——不确定究竟你想做什么和你分派器的例子,你可能想将调度员c#一侧)。
- Create a Windows Runtime component library.
- 创建一个Windows运行时组件库。
- Define a
public interface class
in a C++/CX header for the C# to implement (C++ to call) (e.g.ICallback
). - 在c++ /CX header中定义一个公共接口类,用于c#实现(c++调用)(例如,ICallback)。
- Define a
public ref class
in a C++/CX header for the C++ to implement (C# to call) (e.g.CppCxClass
). - 在c++ /CX header中定义一个公共ref类,用于c++实现(c#调用)(例如CppCxClass)。
-
Add a method in
CppCxClass
that passes and stores anICallback
. (A C++ global variable is shown for consiseness, I recommend you review this to see if you can find a better place to store this in your code-base).在CppCxClass中添加一个方法,该方法传递并存储ICallback。(一个c++全局变量被显示出来,我建议您查看一下,看看是否可以找到更好的地方将它存储在代码库中)。
ICallback^ globalCallback; ... void CppCxClass::SetCallback(ICallback ^callback) { globalCallback = callback; }
-
Reference the WinRT library in your C# code.
在c#代码中引用WinRT库。
- C# code: create an instance of
CppCxClass
usingvar cppObject = new CppCxClass()
. - c#代码:使用var cppObject = new CppCxClass()创建CppCxClass实例。
- C# code: create a class which implements
ICallback
(e.g.CSharpCallbackObject
). - 代码:创建一个实现ICallback的类(例如CSharpCallbackObject)。
- C# code: pass an instance of
CSharpCallbackObject
down to C++. E.g.cppObject.SetCallback(new CSharpCallbackObject())
. - 代码:将CSharpCallbackObject的一个实例传递给c++。例如cppObject。SetCallback(新CSharpCallbackObject())。
You can now call C# with globalCallback->CallCsharp(L"Hello C#");
. You should be able to extend either ICallback
and/or CppCxObject
to do the rest of your tasks.
您现在可以使用globalCallback->CallCsharp(L“Hello c#”)调用c#;您应该能够扩展ICallback和/或CppCxObject来完成剩下的任务。
#2
16
After a lot of headaches trying to figure out the required code, I think it's worth posting the final version here
在尝试找出所需的代码之后,我认为在这里发布最终版本是值得的
C++/CX
c++ /残雪
//.h
[Windows::Foundation::Metadata::WebHostHidden]
public interface class ICallback
{
public:
virtual void Exec( Platform::String ^Command, Platform::String ^Param);
};
//.cpp
ICallback ^CSCallback = nullptr;
void Direct3DInterop::SetCallback( ICallback ^Callback)
{
CSCallback = Callback;
}
//...
if (CSCallback != nullptr)
CSCallback->Exec( "Command", "Param" );
C#
c#
public class CallbackImpl : ICallback
{
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(() => {
// //Lambda code
//}
}
}
//...
CallbackImpl CI = new CallbackImpl();
D3DComponent.SetCallback( CI);
#1
20
By getting an object in C# code to implement a Windows RT interface, and passing down a reference to this object, it is possible to do all of the above with a bit of set-up (if I understand correctly - not sure about exactly what you want to do with your Dispatcher examples - you might want to wrap the Dispatcher on the C# side).
通过一个对象在c#代码实现一个Windows RT接口,并对该对象的引用,可以做所有以上的设置(如果我理解正确的话——不确定究竟你想做什么和你分派器的例子,你可能想将调度员c#一侧)。
- Create a Windows Runtime component library.
- 创建一个Windows运行时组件库。
- Define a
public interface class
in a C++/CX header for the C# to implement (C++ to call) (e.g.ICallback
). - 在c++ /CX header中定义一个公共接口类,用于c#实现(c++调用)(例如,ICallback)。
- Define a
public ref class
in a C++/CX header for the C++ to implement (C# to call) (e.g.CppCxClass
). - 在c++ /CX header中定义一个公共ref类,用于c++实现(c#调用)(例如CppCxClass)。
-
Add a method in
CppCxClass
that passes and stores anICallback
. (A C++ global variable is shown for consiseness, I recommend you review this to see if you can find a better place to store this in your code-base).在CppCxClass中添加一个方法,该方法传递并存储ICallback。(一个c++全局变量被显示出来,我建议您查看一下,看看是否可以找到更好的地方将它存储在代码库中)。
ICallback^ globalCallback; ... void CppCxClass::SetCallback(ICallback ^callback) { globalCallback = callback; }
-
Reference the WinRT library in your C# code.
在c#代码中引用WinRT库。
- C# code: create an instance of
CppCxClass
usingvar cppObject = new CppCxClass()
. - c#代码:使用var cppObject = new CppCxClass()创建CppCxClass实例。
- C# code: create a class which implements
ICallback
(e.g.CSharpCallbackObject
). - 代码:创建一个实现ICallback的类(例如CSharpCallbackObject)。
- C# code: pass an instance of
CSharpCallbackObject
down to C++. E.g.cppObject.SetCallback(new CSharpCallbackObject())
. - 代码:将CSharpCallbackObject的一个实例传递给c++。例如cppObject。SetCallback(新CSharpCallbackObject())。
You can now call C# with globalCallback->CallCsharp(L"Hello C#");
. You should be able to extend either ICallback
and/or CppCxObject
to do the rest of your tasks.
您现在可以使用globalCallback->CallCsharp(L“Hello c#”)调用c#;您应该能够扩展ICallback和/或CppCxObject来完成剩下的任务。
#2
16
After a lot of headaches trying to figure out the required code, I think it's worth posting the final version here
在尝试找出所需的代码之后,我认为在这里发布最终版本是值得的
C++/CX
c++ /残雪
//.h
[Windows::Foundation::Metadata::WebHostHidden]
public interface class ICallback
{
public:
virtual void Exec( Platform::String ^Command, Platform::String ^Param);
};
//.cpp
ICallback ^CSCallback = nullptr;
void Direct3DInterop::SetCallback( ICallback ^Callback)
{
CSCallback = Callback;
}
//...
if (CSCallback != nullptr)
CSCallback->Exec( "Command", "Param" );
C#
c#
public class CallbackImpl : ICallback
{
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(() => {
// //Lambda code
//}
}
}
//...
CallbackImpl CI = new CallbackImpl();
D3DComponent.SetCallback( CI);