This time, the mvvm light event is bound. Mvvm light provides great support for event binding. It can be implemented in many ways. Commands can be directly bound to events that support command, such as button click events. For events that do not support command, such as textchanged, you can use invokecommandaction or eventtocommand to bind the event. I will not start it here, because I am also knowledgeable, so as not to mislead the audience. This demonstration uses eventtocommand to bind the hubtile's tap event.
Define a relaycommand in viewmodel:
Namespace Dbfm7.viewmodel
{
Public class Channeltileviewmodel : Viewmodelbase
{
/// <Summary>
/// Initializes a new instance of the channeltileviewmodel class.
/// </Summary>
Public Channeltileviewmodel ()
{
Touchpanel . Enabledgestures = Gesturetype . Tap | Gesturetype . Horizontaldrag;
This. _ Tapcommand =NewRelaycommand<String> (This. Hubtile_tap );
}
PrivateRelaycommand<String> _ Tapcommand;
Public Relaycommand < String > Tapcommand
{
Get { Return _ Tapcommand ;}
Set
{
_ Tapcommand = Value ;
This . Raisepropertychanged ( "Tapcommand" );
}
}
Private voidHubtile_tap (StringHubtile)
{
MessageBox. Show (hubtile );
}
}
}
Add xmlns in view:
Xmlns:I= "CLR-namespace: system. Windows. interactivity; Assembly = system. Windows. Interactivity"Xmlns:CMD= "CLR-namespace: galasoft. mvvmlight. Command; Assembly = galasoft. mvvmlight. Extras. wp71"
Bind a tap:
< Toolkit : Hubtile Grid. Row = "0" Grid. Column = "1" Margin =" X : Name = "Om" > < I : Interaction. triggers > < I : Eventtrigger Eventname = "Tap"> < CMD : Eventtocommand Command = "{ Binding Tapcommand }" Commandparameter = "{ Binding Title , Elementname = Om} "/> </ I : Eventtrigger > </ I : Interaction. triggers > </ Toolkit : Hubtile >
The command binding command of eventtocommand. commandparameter is bound to the passed parameter. Passeventargstocommand can pass the ARGs of the event.
I originally wanted to pass the sender of the event in the past. Although it can be implemented, it is too cumbersome and it is not in line with the mvvm idea in Foreign Forums.
My stance on mvvm is:
View: knows about viewmodel
Viewmodel: knows about model (has no knowledge of view)
Model: holds data. (has no knowledge of viewmodel, view)
Therefore, it is not appropriate to pass the sender to the VM.
For more information about event binding, see: http://geekswithblogs.net/lbugnion/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx
In the next section, I will use the message mechanism of mvvm light in WP7 to implement page navigation.