using MVVM light messenger with Silverlight 4 ChildWindow dialog class -


greetings! enjoying using mvvm light -great framework - has made life easier, , has removed number of barriers proving difficult overcome....

question:

i attempting setup custom dialog box editing messages users send each other. attempting construct silverlight custom dialog box using childwindow object using mvvm framework.

was wondering if there suggestions how might accomplished

following dialog mvvm sample code found here: http://mvvmlight.codeplex.com/thread/view.aspx?threadid=209338 got stuck because childwindow dialog object in silverlight async, , has different result class.

so - basic idea have using view model of class (in case matrix.messageviewmodel) create instance of custom dialog box, send through messenger.send<>, process registered message in view display dialog, have childwindow dialog box's save button handler fire messenger.send modified contents stored using save method on viewmodel...

seems bit round-about - wanted make sure there wasn't cleaner way....

relevant code bits:

view model:

messagedialogbox = new messageeditordialog(     selectedmessage, this.selectedsiteid,  this.loggedonemployee.id, this.projects);  dialogmessage editmessage = new dialogmessage(     this, messagedialogbox,"edit message", dialogmessagecallback); messenger.default.send(editmessage); 

view:

public viewhost() {     initializecomponent();      loaded += new routedeventhandler(viewhost_loaded);      if (!viewmodelbase.isindesignmodestatic)     {         // use mef load view model         compositioninitializer.satisfyimports(this);     }      applicationmessages.isbusymessage.register(this, onisbusychange);      messenger.default.register<dialogmessage>(this, msg => showdialog(msg)); }    private void showdialog(dialogmessage msg) {     messageeditordialog mydialog = (messageeditordialog) msg.target;     mydialog.show(); } 

dialog save:

private void buttonsave_click(object sender, routedeventargs e) {     messenger.default.send<message>(         this.messageitem, commandmessages.messagetypes.messagesave); } 

this ties viewmodel, has messenger.default.register<> watching commandtypes.messagesave routes resulting messageitem model storage.....

that's pretty darn close i'd do, except there couple of things differently.

  1. i'd have view model dialog view, , move messaging logic rather view's code behind.
  2. i'd use save command in view model, , bind buttonsave command. moves save logic view model instead of code behind of view.
  3. you're using different message when save button clicked. also, you're not using dialogmessage's callback. assuming change using save command, save message in private member in view model, use message's callback when user saves.
  4. you may want think re-using dialog view, or ensuring view being cleaned correctly don't end memory leak.

here's changes i'd make view model following suggestions 2 & 3.

public class messageeditordialogviewmodel : viewmodelbase {     private dialogmessage _dialogmessage;      public relaycommand savecommand { get; private set; }      public dialogmessage message { get; set; }      public messageeditordialogviewmodel()     {         savecommand = new relaycommand(savecommandexecute);     }      private savecommandexecute()     {         message.execute();     } } 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -