WinRT component and event RRS feed

  • Question

  • I have a WinRT C++ component which work fine when use in C#.
    (implementation below)

    However, when I tried to use it in a Direct3D C++ WinRT app it crashes in the constructor, while calling
    ref new PerformanceTimer()
    -> Reset()
    -> OnPropertyChanged("Total")
    -> PropertyChanged(this, ....)

    I got a "Platform::WrongThreadException ^"

    What can I do?
    I need those event to notify about some important change (this Total property is not important, but others are)

    public ref class PerformanceTimer sealed :  Windows::UI::Xaml::Data::INotifyPropertyChanged
    	void Reset()
    		m_total = 0.0f;
    	property float Total { float get() { return m_total; } }
    #pragma region INotifyPropertyChanged
            void OnPropertyChanged(Platform::String^ propertyName)
    			PropertyChanged(this, ref new Windows::UI::Xaml::Data::PropertyChangedEventArgs(propertyName));
    		event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ _pChanged;
    		virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged
    			Windows::Foundation::EventRegistrationToken add(Windows::UI::Xaml::Data::PropertyChangedEventHandler^ value)
    				return _pChanged::add(value);
    			void remove(Windows::Foundation::EventRegistrationToken token)
    			void raise(Object^ sender, Windows::UI::Xaml::Data::PropertyChangedEventArgs^ e)
    				_pChanged::raise(sender, e);
    #pragma endregion	
    	LARGE_INTEGER m_frequency;
    	float m_total;

    Tuesday, December 11, 2012 3:49 PM

All replies

  • Hi,

    How about remove the add, remove and raise function?
    I think we should write these function in this way.

                // in c++, it is not neccessary to include definitions of add, remove, and raise.
                //  these definitions have been made explicitly here so that we can check if the 
                //  event has listeners before firing the event
                virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged
                    virtual Windows::Foundation::EventRegistrationToken add(Windows::UI::Xaml::Data::PropertyChangedEventHandler^ e)
                        _isPropertyChangedObserved = true;
                        return _privatePropertyChanged += e;
                    virtual void remove(Windows::Foundation::EventRegistrationToken t)
                        _privatePropertyChanged -= t;
                    virtual void raise(Object^ sender, Windows::UI::Xaml::Data::PropertyChangedEventArgs^ e)
                        if (_isPropertyChangedObserved)
                            _privatePropertyChanged(sender, e);

    Best regards,

    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 13, 2012 6:42 AM
  • Hi Jess, indeed I was confused at some stage by some compilation error (C++ is a bit tough on newbie with kilometers of error for a misspell.. :~)

    As it turns out I don't need explicit definition of the event at all!
    But this was instructive! :)

    Anyway, as it is the problem remains....
    I have a sample at this location:


    Everything seems to work ok, but it's because in the C++ sample (Sample0_DXCoreWindow) I worked around 2 problems:

    - use the event by modifying
    DXBase\Scene.cpp:26, remove "if(w)" so that the event handler is always added => crash

    - fire event even without listener by modifying
    DXBase\PerformanceTimer.h:103 use the PROPERTY_CHANGE_EVENT_AUTO macro (instead of PROPERTY_CHANGE_EVENT_EXPLICIT) => app crash

    Thursday, December 13, 2012 10:52 AM