在 WP8 中从 C++ 代码调用 C# 方法 [英] Calling C# method from C++ code in WP8

查看:27
本文介绍了在 WP8 中从 C++ 代码调用 C# 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对从 Windows Phone 8 中的 C++ 代码调用 C# 方法很感兴趣.我已经学会了如何通过 C++ 代码中的委托声明将回调函数从 C# 传递给 C++ 代码,但我想看看是否我可以执行以下任何操作:

  • 直接从 C++ 代码调用某些方法.这将涉及以某种方式检查 C++ 中的 C# 对象构成,这对我来说似乎不太可能,但我想我还是会问你们所有人

  • 在 C# 代码中触发事件,然后可以由 C# 方法处理

  • 使用调度器在主 UI 线程中调用 C# 回调,以便回调可以修改 UI 元素

  • 在C#代码中使用dispatcher触发事件,(本质上是以上两点的合并)

简而言之,我正在寻找尽可能多的 C++ --> C# 通信技巧,你们可以扔给我,我想全部学习.:)

解决方案

通过在 C# 代码中获取一个对象来实现 Windows RT 接口,并向下传递对该对象的引用,可以使用一些设置(如果我理解正确 - 不确定您想用 Dispatcher 示例做什么 - 您可能希望将 Dispatcher 包装在 C# 端).

  1. 创建 Windows 运行时组件库.
  2. 在 C++/CX 标头中定义一个 公共接口类,以便 C# 实现(C++ 调用)(例如 ICallback).
  3. 在 C++/CX 头文件中定义一个 public ref class 以供 C++ 实现(C# 调用)(例如 CppCxClass).
  4. CppCxClass 中添加一个方法,用于传递和存储 ICallback.(为了简洁起见,显示了 C++ 全局变量,我建议您查看此内容,看看是否可以找到更好的位置将其存储在代码库中).

    <块引用>

    ICallback^ globalCallback;...void CppCxClass::SetCallback(ICallback ^callback){globalCallback = 回调;}

  5. 在 C# 代码中引用 WinRT 库.

  6. C# 代码:使用 var cppObject = new CppCxClass() 创建 CppCxClass 的实例.
  7. C# 代码:创建一个实现 ICallback 的类(例如 CSharpCallbackObject).
  8. C# 代码:将 CSharpCallbackObject 的实例传递给 C++.例如.cppObject.SetCallback(new CSharpCallbackObject()).

您现在可以使用 globalCallback->CallCsharp(L"Hello C#"); 调用 C#.您应该能够扩展 ICallback 和/或 CppCxObject 来完成其余的任务.

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:

  • 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

  • Trigger events in the C# code, which can then be handled by C# methods

  • Use a dispatcher to call C# callbacks in the Main UI thread so that the callbacks can modify UI elements

  • Use a dispatcher to trigger events in the C# code, (Essentially a merging of the above two points)

In short, I am looking for as many C++ -->C# communication tips as you guys can throw me, I want to learn it all. :)

解决方案

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).

  1. Create a Windows Runtime component library.
  2. Define a public interface class in a C++/CX header for the C# to implement (C++ to call) (e.g. ICallback).
  3. Define a public ref class in a C++/CX header for the C++ to implement (C# to call) (e.g. CppCxClass).
  4. Add a method in CppCxClass that passes and stores an ICallback. (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).

    ICallback^ globalCallback;
    ...
    void CppCxClass::SetCallback(ICallback ^callback)
    {
        globalCallback = callback;
    }
    

  5. Reference the WinRT library in your C# code.

  6. C# code: create an instance of CppCxClass using var cppObject = new CppCxClass().
  7. C# code: create a class which implements ICallback (e.g. CSharpCallbackObject).
  8. C# code: pass an instance of CSharpCallbackObject down to C++. E.g. cppObject.SetCallback(new 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.

这篇关于在 WP8 中从 C++ 代码调用 C# 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆