Viewmodel’s communication: return data to previous screen

Asked

Viewed 148 times

3

I have a screen A that has a text field and a button, that screen button A, opens a second screen B.

That screen B has another text field to fill and save, when the person click save the screen closes, and what she filled on the screen B must appear filled in the screen field A.

I used canvas A and B as an example, how to do using MVVM ?

I found this question, but what I want to do is the opposite of MVVM.

  • Are you using any WPF framework? Which?

  • @ramaral I’m using Prism but if you make an example without Prism also accepted.

1 answer

2


One possibility is to use the Event Aggregator Pattern.

Write a class for the event:

public class TelaBResultEvent : PubSubEvent<string>{}

In the Viewmodel maker of the screen A subscribe the event:

eventAggregator.GetEvent<TelaBResultEvent>().Subscribe(OnTelaBResult);
  • eventAggregator is a IEventAggregator injected into the manufacturer.
  • OnTelaBResult is a Viewmodel method with signature

    public void OnTelaBResult(string telaBResult)
    

    that will be called when the event is received.
    Use the value received in the parameter telaBResult in the text field.

In Viewmodel of screen B publish the event when save/close the screen, passed the value of the text field.

_eventAggregator.GetEvent<TelaBResultEvent>().Publish("Texto a publicar");
  • _eventAggregator is a field/property to which a IEventAggregator injected into the Viewmodel constructor.
  • It worked perfectly, thank you very much, another question, besides this way you know another? or does it not exist? you can tell me.

  • 1

    Perhaps there will be others. However this is the way advocated by Prism(and other WPF frameworks) for communication between Viewmodel’s.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.