wpf - MVVM light, Messages from mediator pattern should be killed manually? -


i developing project , using mediator pattern communication between viewmodel , view.

the problem that, method registered message executed many times message sent.

well, lets write down problem.

from simple menu have item have assign command

  //mainwindow.xaml <awc:imagebutton istoolstyle="true"  orientation="vertical" imagesource="" command="{binding showpriceswindowcommand}">prices</awc:imagebutton>    //mainwindow viewmodel  public icommand showpriceswindowcommand {         { return new relaycommand(showpriceswindowexecute); }     } void showpriceswindowexecute() {         messenger.default.send(new notificationmessage<hotel>(this, selectedhotel, "showpriceswindow"),                                "showpriceswindow");     }   //mainwindow.xaml.cs messenger.default.register<notificationmessage<hotel>>(this, "showpriceswindow", hotelpricemessagereceived);  private void hotelpricemessagereceived(notificationmessage<hotel> selectedhotel) {         var roomprices = new roomprices();//this view has roompriceviewmodel datacontext         roomprices.show();         //via messaging sending selectedhotel object         messenger.default.send(new notificationmessage<hotel>(this, selectedhotel.content, "showpriceswindow"),                                "showpriceswindow2");     } 

from roompricesviewmodel made simple calculation , need close view , afterwards open other one.

public roompricesviewmodel(idialogservice dialogservice) {         this._dialog = dialogservice;         messenger.default.register<notificationmessage<hotel>>(this, "showpriceswindow2", notificationmessagereceived);     }      private void notificationmessagereceived(notificationmessage<hotel> selectedhotel) {         this.selectedhotel = selectedhotel.content;         loadrooms();     }  void loadrooms() {         if (rooms.count == 0) {             dialogresponse = _dialog.showmessage("display message;", "", dialogbutton.yesno, dialogimage.warning);             switch (dialogresponse) {                 case dialogresponse.yes:                     //close roomprices window , open roomtypeswindow                     messenger.default.send(new notificationmessage<hotel>(this, this.selectedhotel, "closewindowandopenroomtypes"), "closewindowandopenroomtypes");                     return;                     break;                 case dialogresponse.no:                     break;             }         }     } 

the code seems work if click on button, view opening, prompts me messagebox , if click yes, current view closed , 1 opening.

if click again button window closed , 2 windows opened instead of one.

if click 10 times, can imagine :)

how prevent this? must kill somehow message?

it seems bad written, have been confused lot messaging(mediator pattern) know if used it, things easier.

i appreciate or advise.

thanks

solution

the problem registered in constructor of window message , many times window opened many times messages registered. hence, window opened many times "notificationmessage" registered.

i simple run

public roompricesviewmodel(idialogservice dialogservice) {         messenger.default.register<notificationmessage<hotel>>(this, "showpriceswindow", notificationmessagereceived);     }      private void notificationmessagereceived(notificationmessage<hotel> selectedhotel) {         //code         messenger.default.unregister(this);     } 

and problem solved.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -