如何在 WP8 中从本机 c++ 与 UI 通信

本文关键字:c++ UI 通信 本机 WP8 | 更新日期: 2023-09-27 18:32:40

我是WP8/c#的新手,新信息超载,肯定缺少一些东西。

我有使用本机代码库的wp8应用程序。如何通知 UI 有关本机代码的更改?我知道我无法将回调从 c# 函数传递到 wp8 上的本机 c++。下面的简单代码说明了我想要什么:

主页 cs(英文)

namespace phoneapp
{
  public partial class MainPage : PhoneApplicationPage
  {
    testRT _testrt;
    public MainPage()
    {
      _testrt= new testRT();
    }
    private void btnTest_Click(object sender, RoutedEventArgs e)
    {
      _testrt->foo();
    }
  }
}

运行时组件 CPP

namespace rtc
{
  public ref class testRT sealed
  {
    public:
      testRT();
      void foo();
    private:
      test* _test;
  };
}
testRT::testRT()
{
  _test= new test();
}
testRT::foo()
{
  _test->foothread();
}

原生类 CPP

test::test()
{
}
test::~test()
{
}
void test::foothread()
{
  signal_UI_status("started");
  while (notfinished)
  {
    //do something
    ...
    signal_UI_results("found %i results", res);
  }
  signal_UI_status("finished");
}

在Windows Phone上实现这种"信号"功能的最佳方法是什么?回调?命名管道?套接字?

如何在 WP8 中从本机 c++ 与 UI 通信

可以在 Windows 运行时中声明回调委托,并将 C# 函数传递回该委托。

public delegate void WinRtCallback(Platform::String^ resultString);
public ref class testRT sealed
{
public:
  testRT();
  void foo(WinRtCallback^ callback);
private:
  test* _test;
};

或者更好的是,你可以让你的C++返回异步操作(通过IAsyncAction和其他异步接口),但这要高级得多。

有关此内容的详细信息,请参阅MSDN,如"在C++中为Windows 应用商店应用程序创建异步操作",该操作也适用于Windows Phone应用程序。