Mvvm reconstruction record of WP7 client of Douban station using mvvm light for event binding

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.