如何在WP8中使用本机c ++与UI交谈?

时间:2013-09-25 09:50:12

标签: c# c++ windows-phone-8

我是WP8 / c#的新手,重载了新信息,并且确实遗漏了一些东西。

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

MainPage 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上实现此类“信号”功能的最佳方法是什么?打回来? namedpipe?插座?

1 个答案:

答案 0 :(得分:0)

您可以在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上显示为"Creating Asynchronous Operations in C++ for Windows Store Apps",也适用于Windows Phone应用。