User avatar
DiNWw
Topic Author
Posts: 4
Joined: 17 Nov 2020, 08:27

Change viewmodel from outside. C++

26 May 2021, 14:23

Good afternoon.
I'm trying to use NoesisGui to create a standalone windows program. I am using NoesisApp c++. There is a viewmodel with boolean parameters and an xaml with grids that change colours depending on the model. My misunderstanding is how can I change model from outside? For example calculate every frame and change model like Timer.Tick Event - on winform. Or a function that would check every frame to see if new data is coming from the network and fill it into the model.

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

Re: Change viewmodel from outside. C++

26 May 2021, 20:34

In your application you will have a main window, and either the application or the window code-behind will create and set the global viewmodel. So you know and own the viewmodel and can change it whenever you want. Could you please elaborate a bit more what are you having problems with? Is it with modifying the viewmodel or how to trigger something like a Timer?
 
Ahept
Posts: 2
Joined: 14 Oct 2020, 16:41

Re: Change viewmodel from outside. C++

27 May 2021, 08:28

The problem we are trying to solve is to update viewmodel when receiving data from network in the different thread. So we try to set the timer in the application to retrieve network messages, synchronize and update the model. Also we'd like to do something like getting current system time.
The second question, is there any Update callback in Noesis so that we could use it in cross-platform applications?
 
User avatar
sfernandez
Site Admin
Posts: 2207
Joined: 22 Dec 2011, 19:20

Re: Change viewmodel from outside. C++

27 May 2021, 20:29

The UI tree can only be updated on the same thread where the UI elements are created, so the data received from the network thread should be enqued to process it in the UI thread. If you are using our application framework, one option could be to get the View of the main window, and then use the Rendering event (that is called every frame) to process that queue of pending updates you want to apply to the viewmodels. Could that work for you?
 
Ahept
Posts: 2
Joined: 14 Oct 2020, 16:41

Re: Change viewmodel from outside. C++

28 May 2021, 09:20

Thanks for the answer.

Could you direct us to a simple tutorial (or example code piece) to use the Render event, please? It seems that we don't have access to Render event in the MainWindow class (descendant of NoesisApp::Window).
 
User avatar
DiNWw
Topic Author
Posts: 4
Joined: 17 Nov 2020, 08:27

Re: Change viewmodel from outside. C++

28 May 2021, 11:48

Yes, we were able to use the Rendering event for our purposes.
//other includes
#include <NsGui/IView.h>

MainWindow::MainWindow()
{
    Loaded() += MakeDelegate(this, &MainWindow::onLoaded);
    Closed() += MakeDelegate(this, &MainWindow::onClosed);
    initializeComponent();
}
void
MainWindow::onLoaded(BaseComponent*, const Noesis::RoutedEventArgs&)
{
    GetView()->Rendering() += MakeDelegate(this, &MainWindow::update);
    SetDataContext(vm);
    network = std::thread([&]()
    {
        while (!exit)
        {
            proto::toFaltStation protoMsg;
            if (m_boardComputerLink.receiveMessage(protoMsg))
            {
                // parse new message
            }
        }
    });
}

void
MainWindow::update(Noesis::IView*)
{
    //update [b]vm[/b] new values
}
Thank you, we'll keep learning
 
User avatar
sfernandez
Site Admin
Posts: 2207
Joined: 22 Dec 2011, 19:20

Re: Change viewmodel from outside. C++

28 May 2021, 13:33

Great, thanks for the update. I'm marking this as solved.

Who is online

Users browsing this forum: Google [Bot] and 1 guest