简体   繁体   中英

How to talk to UI from native c++ in WP8?

I am new to WP8/c#, overloaded with new information and definetly missing something.

I have wp8 application that uses native code library. How can i notify UI about changes from native code? I understood that i cannot pass callbacks from c# function to to native c++ on wp8. Simple code below illustrates what i want:

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();
    }
  }
}

runtime component cpp

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

native class 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");
}

What is the best way to implement such "signal" functionality on windows phone? callback? namedpipe? socket?

You could declare a callback delegate in Windows Runtime and pass a C# function back to it.

public delegate void WinRtCallback(Platform::String^ resultString);

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

Or even better you could get your C++ to return an asynchronous operation (via IAsyncAction and other async interfaces), but that's much more advanced.

More information about that is on MSDN as "Creating Asynchronous Operations in C++ for Windows Store Apps" , which applies to Windows Phone apps as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM