项目中Slilverlight使用MVVM模式时View的用户交互事件使用的SLExtensions 中的Commands来实现调用ViewModel的事件处理方法。很自然的想到在ViewModel的构造方法中来注册事件处理方法。然而SLExtensions中的Commands采用的是类似静态事件的机制,若程序中有多个同一类型的ViewModel的实例时,事件将被触发多次(等于view modle实例数)因为,每个viewmodel的实例化时都订阅了一遍事件。
看到SLExtension讨论组中有人提到类似问题
,该项目成员titaye的回答是:
I think you missunderstood how the command pattern is working. Command
is just a static event that can be raise from xaml. It allows a better
separation betweenbusiness logicand UI. When you add Input:CommandService.Command="MyButtonClick"
in your xaml, it will attach a behavior to the xaml control raising the
executed command when mouse down is pressed. When the it's raised it
will notify all its subscribers. So as you said you are attaching an
event handler for each button you are notified several times per click.
The code to be invoked on command executionshould be placed in a class
managing your business logic. I often use "Controller" class in my
samples to handle this logic (you can see the showcasecontroller in the
source). This class is created one time andattaches an handler tothe
executed event. In yourbusiness logic, you can pass additional data by
using the Input:CommandService.CommandParameter on you xaml declaration.
看来这个Command处理方法他是在一个唯一实例的controller中使用的,这不太方便于是回复:
You have a singleton controller to prevent the static commands from
broadcasting multiple times which is not expected in business logic,
but I need viewmodel to handle the commands requested from views
because viewmodels have the state of the views and operations to handle
the requests. So viewmodel have to subscribe the commands (static
events) and in some cases, I have multiple viewmodels of same type
instantiated, then would run into the big problem of same command was
handled multiple times (as many as viewmodels' instances) - I know this
is not the way that commands should be used as they are static events,
but indeed I need viewmodels to subscribe their views' commands not a
controller to do this
准备将项目中的Commands机制换成Prism的DelegateCommand。其他Command机制包括:
SimpleCommand
RelayCommand
有人做了一个简单的总结:http://dotnet.org.za/rudi/archive/2009/03/05/the-power-of-icommand.aspx
分享到:
相关推荐
Trusted Platform Module Library Part 3: Commands This TPM 2.0 Part 3 of the Trusted Platform Module Library specification contains the definitions of the TPM commands. These commands make use of the ...
此标准规定了SCSI Block Commands - 3(SBC-3)命令集的功能要求.SBC-3允许SCSI块逻辑单元(如刚性磁盘)连接到计算机并提供其使用的定义。 该标准与SCSI Block Commands(SBC-2)命令集INCITS 405-2005保持高度兼容...
Linux Commands
SCSI Primary Commands - 4 (SPC-4) spc4r25 This is an internal working document of T10, a Technical Committee of Accredited Standards Committee INCITS (InterNational Committee for Information ...
ZPL II Commands for RFID
Laravel开发-commands 这是为帮助开发工作中的每个人而创建的Laravel Artisan命令集合。
Unix shell commands,Unix shell commands
FreeBSD Commands It is useful!
ansys commands reference,学习ansys必备,可以熟练运用
Oracle DBA commands
Kingkong AT Commands Spec V1.5
commom commands of latex
SCSI Block Commands – 3
db2 commands to use when operate db2 database.zip
使用想服务器上传jsp执行windows或者linux命令,来获取服务器的资源情况。 Commands with JSP.sjp
Quectel_EC20_AT_Commands_Manual_V1.0.pdfQuectel_EC20_AT_Commands_Manual_V1.0.pdf
摩托罗拉G24 GSM模块AT命令手册 This manual is intended for software developers who communicate with the G24 ...AT commands, and create applications to communicate with the G24 device using the AT commands
UNIX Commands for Beginners,向学习linux的同学下载学习。
FIBOCOM_H3-Family AT Commands User Manual_V1.2.6
1. Added following new AT commands: AT+CGAUTH/AT+NPIN/AT+NCSEARFCN 2. Updated description of parameters for AT+NPSMR/ AT+QSECSWT/AT+QSETPSK/AT+QLWULDATAEX 3. Added maximum response time for all AT ...