【问题标题】:How to talk to UI from native c++ in WP8?如何在 WP8 中从原生 c++ 与 UI 对话?
【发布时间】:2013-09-25 09:50:11
【问题描述】:

我是 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 上实现这种“信号”功能的最佳方式是什么?打回来?命名管道?插座?

【问题讨论】:

  • Howto implement callback interface from unmanaged DLL to .net app? 的可能重复项 - 可能有更好的匹配,但这已被问过很多次,在 c++/c#/callback 上搜索会发现很多噪音
  • 嗯,不完全是——它不是 .net 应用程序:windows phone 8 通过 c++/cx 中的 windows phone 运行时组件使用本机库

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


【解决方案1】:

您可以在 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 应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    相关资源
    最近更新 更多