wpf - Do you need all those properties in MVVM? -


i started using mvvm , figured out how work commands of http://www.dotmaniac.net/wpf-karl-shifletts-relaycommand/ , http://www.codeproject.com/articles/126249/mvvm-pattern-in-wpf-a-simple-tutorial-for-absolute. succeeded simple test work.

below here code put taken these resources mentioned. clarify, code post working. think it's not compact useful. have application in progress has properties linked textboxes,labels,buttons,datagrids, ... events if click on button aren't yet. therefore code below. reason why post code follow:

  1. can code private _oshowmsgbox icommand shorter? looks of need 2 subs or functions same in 1.

    private _oshowmsgbox icommand = new relaycommand(new action(of object)(addressof showmsgboxsub),                                                      new predicate(of object)(function() if(textboxtext = "", false, true))) 
  2. is necessary have properties in file (properties commands , binding data controls)? application i'm working on has on 150 controls (textbox,label,button,datagrid) code getting big pretty fast , looks it's inefficient.

below line snapshot of working code in project. see tiny part of have in total right now.


my xaml:

<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:mvvm_test" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" x:class="mainwindow" title="mainwindow" height="350" width="525">     <window.resources>         <local:viewmodel x:key="viewmodeldatasource" d:isdatasource="true" />     </window.resources>     <grid datacontext="{binding source={staticresource viewmodeldatasource}}">         <button content="{binding textboxtext}" command="{binding showmsgbox}"/>         <textbox text="{binding textboxtext, updatesourcetrigger=propertychanged}" />     </grid> </window> 

for first time succeeded have no code in code behind file.

this relaycommand came with:

public class relaycommand implements icommand  #region "fields" private readonly _execute action(of object) private readonly _canexecute predicate(of object) #end region  #region "constructors" public sub new(byval execute action(of object))     me.new(execute, nothing) end sub  public sub new(byval execute action(of object), byval canexecute predicate(of object))      if execute nothing         throw new argumentnullexception("execute")     end if      _execute = execute     _canexecute = canexecute  end sub  #end region  #region "icommand members"  public function canexecute(parameter object) boolean implements icommand.canexecute     return if(_canexecute nothing, true, _canexecute(parameter)) end function   public custom event canexecutechanged eventhandler implements icommand.canexecutechanged      addhandler(byval value eventhandler)         addhandler commandmanager.requerysuggested, value     end addhandler      removehandler(byval value eventhandler)         removehandler commandmanager.requerysuggested, value     end removehandler      raiseevent(byval sender system.object, byval e system.eventargs)      end raiseevent  end event public sub execute(parameter object) implements icommand.execute     _execute(parameter) end sub  #end region  end class 

then in viewmodel have this:

imports system.componentmodel imports system.runtime.compilerservices  public class viewmodel implements inotifypropertychanged  private _stext string public property textboxtext string             return _stext     end     set(byval value string)         _stext = value         raisepropertychanged()     end set end property  public event propertychanged(sender object, e propertychangedeventargs) implements inotifypropertychanged.propertychanged  protected sub raisepropertychanged(<callermembername()> optional byval propertyname string = nothing)     raiseevent propertychanged(me, new propertychangedeventargs(propertyname)) end sub  private _oshowmsgbox icommand = new relaycommand(new action(of object)(addressof showmsgboxsub), new predicate(of object)(function() if(textboxtext = "", false, true))) public property showmsgbox icommand             return _oshowmsgbox     end     set(byval value icommand)         _oshowmsgbox = value     end set end property  public sub showmsgboxsub()     messagebox.show(textboxtext) end sub  end class 

for commands, suspect in case readonly, should able use vb.net's auto property feature:

public property showmsgbox _  new relaycommand( _    new action(of object)(addressof showmsgboxsub), _    new predicate(of object)(function() if(textboxtext = "", false, true))) 

for bindable properties change need use verbose property syntax , signal property has changed in setter firing propertychanged event on view model class implements inotifypropertychanged.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -