___ erkimt ___ Communication between ViewModel's: return data to previous screen ______ qstntxt ___
I have a A screen that has a text field and a button, this button on the A screen, opens a second screen B .
This B screen has another text field to populate and save, when the person clicking save saves the screen closes, and the one they filled in the B screen has to appears populated in the A screen field.
I used the A and B screen as an example, how to do this using MVVM ?
I found this question , but what I want to do is the inverse with MVVM .
One possibility is to use the Event Aggregator Pattern .
Write a class for the event:
public class TelaBResultEvent : PubSubEvent<string>{}
In the constructor of the screen The ViewModel subscribes the event:
eventAggregator.GetEvent<TelaBResultEvent>().Subscribe(OnTelaBResult);
-
eventAggregator
is IEventAggregator
injected into the constructor.
-
OnTelaBResult
viewmodel is a method with the signature
public void OnTelaBResult(string telaBResult)
that will be called when the event is received.
Use the value received in the telaBResult
parameter in the text field.
On the screen ViewModel B post the event when saving / closing the screen, after the value of the text field.
_eventAggregator.GetEvent<TelaBResultEvent>().Publish("Texto a publicar");
-
Co_de%% is a field / property to which it was assigned a
_eventAggregator
injected into the ViewModel constructor.