WinRT C++: Raising PropertyChanged event on a GUI Thread

If you are a WPF developer you don’t have to raise the PropertyChanged even on a GUI Thread, but it seems as though same pattern in C++ WinRT Windows Store component requires this even to be raised on a GUI Thread.

I happened to stuble upon another blog (http://w8md.wordpress.com/) which helped me with my Unit Testing (http://w8md.wordpress.com/2012/09/26/async-unit-tests-in-ccx/).  It happened to refer to a project on CodePlex called Hilo.  Looking at the source code in the TestHelper.cpp class (TestHelper.cpp) I found helpful code.  Adapted it below:

In the .h file:


namespace RSSParser
{
  [Windows::Foundation::Metadata::WebHostHidden]
  [Bindable]
  public ref class ViewModel sealed : public INotifyPropertyChanged
...
  protected:
    void RaisePropertyChanged(String^ propertyName);

In the CPP file:


void ViewModel::RaisePropertyChanged(String^ property)
{
     CoreDispatcher^ dispatcher = CoreApplication::MainView->CoreWindow->Dispatcher;
     dispatcher->RunAsync(CoreDispatcherPriority::Normal,
       ref new Windows::UI::Core::DispatchedHandler([=] () -> void
      {
       PropertyChanged(this, ref new PropertyChangedEventArgs(property));
      }));
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s