wpf - Passing multiple parameters to Prism's EventAggregator -


i'm using prism's eventaggregator loosely coupled communication between module's viewmodels. have have several properties (e.g. firstname, lastname) in viewmodela need update properties in viewmodelb when values change. current solution involves:

viewmodela publishes event new value firstname payload:

 public string firstname     {         {return firstname;}         set          {             this.firstname = value;             eventaggregator.getevent<patientdetailsevent>().publish(firstname);         }     } 

viewmodelb subscribed event , changes firstname property accordingly:

public patientbannerviewmodel(ieventaggregator eventaggregator)     {         this.eventaggregator = eventaggregator;         eventaggregator.getevent<patientdetailsevent>().subscribe(updatebanner, threadoption.uithread);     }      public void updatebanner(string firstname)     {         this.firstname = firstname;     } 

this works fine single property. doesn't work multiple, different properties because viewmodelb has no idea property has changed on viewmodela . viewmodelb knows new value is, doesn't know of properties update.

i create separate events each property seems repetitive. seems cleaner use 1 event. ideally, when publishing event, viewmodela should tell viewmodelb property has changed. how can this?

sorry, found answer question in this post. this blog post rachel lim helpful.

what need viewmodela (the publisher) tell viewmodelb (the subscriber) 2 pieces of information:

  1. what property has changed on viewmodela
  2. what new value of property

we need communicate 2 pieces of information (i.e. properties) prism's eventaggregator takes 1 parameter, payload. problem.

to pass multiple pieces of information (properties) via eventaggregator can publish instance of class defines these properties eventaggregator's payload. called class patientdetailseventparameters , defines 2 properties:

public class patientdetailseventparameters {     public string patientproperty { get; set; }     public string value { get; set; } } 

i created class in infrastructure assembly (the same place define events) other assemblies have reference to.

you can publish instance of class payload (instead of string holds 1 value). allows multiple parameters passed payload.

public string firstname     {                  {             return firstname;         }         set          {             this.firstname = value;             eventaggregator.getevent<patientdetailsevent>().publish(new patientdetailseventparameters() {value = firstname, patientproperty = "firstname"});         }     } 

you can see here new instance of patientdetailseventparameters created when patientdetailsevent published. 2 properties value , patientproperty set. patientproperty string tells viewmodelb (i.e. subscriber) property has changed. value new value of property has changed.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -