Steback
Topic Author
Posts: 8
Joined: 28 Jul 2020, 19:58

Change State

19 Nov 2020, 01:35

Hi, I have a state enum class like this:
enum class State {
          Principal,
          Start,
          Settings
};
I have configured buttons that change this state in xaml files. My question is how can I change this property in C ++ code and notify Noesis that it changes?

The reason I want to do it. In my game there are some events that are not related to the GUI, but according to some events I want to change the view that is controlled by the ViewModel class which is very similar to the Menu3D example.

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 2254
Joined: 22 Dec 2011, 19:20

Re: Change State

19 Nov 2020, 10:27

First you need to register the enum in the reflection and the converter in the factory (so you can use the enum values in xaml):
// Header file
NS_DECLARE_REFLECTION_ENUM(YourNamespace::State)

// Cpp file
NS_IMPLEMENT_REFLECTION_ENUM(YourNamespace::State, "YourNamespace.State")
{
    NsVal("Principal", YourNamespace::State::Principal);
    NsVal("Start", YourNamespace::State::Start);
    NsVal("Settings", YourNamespace::State::Settings);
}

// In you application component registration function
void RegisterComponents()
{
  RegisterComponent<EnumConverter<YourNamespace::State>>();
}
Then you expose the property in your ViewModel, that should implement INotifyPropertyChanged in order to notify UI when properties are updated (our application framework provides base class NotifyPropertyChangedBase to simplify that):
class ViewModel: public NoesisApp::NotifyPropertyChangedBase
{
public:
  YourNamespace::State GetState() const { return _state; }
  void SetState(YourNamespace::State value)
  {
    if (_state != value)
    {
      _state = value;
      OnPropertyChanged("State");
    }
  }
  
private:
  YourNamespace::State _state;
  
  NS_IMPLEMENT_INLINE_REFLECTION(ViewModel, NoesisApp::NotifyPropertyChangedBase)
  {
    NsProp("State", &ViewModel::GetState, &ViewModel::SetState);
  }
};
Is this what you are looking for?
 
Steback
Topic Author
Posts: 8
Joined: 28 Jul 2020, 19:58

Re: Change State

19 Nov 2020, 13:45

Yes it is. Thank you!

Who is online

Users browsing this forum: jsteedVR and 4 guests