0

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?

  • possible duplicate of [Howto implement callback interface from unmanaged DLL to .net app?](http://stackoverflow.com/questions/2167895/howto-implement-callback-interface-from-unmanaged-dll-to-net-app) - there's probably a better match, but this has been asked so many times, searching on c++/c#/callback turns up a lot of noise – stijn Sep 25 '13 at 10:51
  • well, not exactly - it's not .net app: windows phone 8 uses native library through windows phone runtime component in c++/cx – user2814565 Sep 25 '13 at 11:13

1 Answers1

0

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.

Paul Annetts
  • 9,554
  • 1
  • 27
  • 43