Keywords: WPF. Net C # mvvm light Toolkit framework Visual Studio 2010 vs2010 expression blend 4 getting started tutorial New Practice
VII. Messenger
A user-friendly logon interface should be able to record the user ID of the last logon to avoid meaningless Repeated input. To achieve this function, I will continue to process the previous login page.
1. In loginviewmodel. in CS, add a relaycommand property loadedcommand as the command object for responding to the view layer page load completion event, and initialize it in the constructor (pageloaded () the specific implementation of the two moveinputfocusto methods involved in the method will be detailed later ).
2. In blend 4, select "behavior" from the list on the "assets" tab, drag "eventtocommand" to [Page], and change the default name to pageloaded.
3. Bind the command attribute of pageloaded to loadedcommand (note that the eventname in the "Trigger" column in the "properties" tab should be loaded), as shown in the following two figures.
4. Assume that there is a loadlastlogininfo () method in the model user class that reads the user ID of the last successful logon from the configuration file every time a new user object is constructed, the loginpage at the view layer determines whether the input focus should be in the user ID input box or password input box based on whether the user ID input box has a value. However, no input box can be bound to the viewmodel layer to identify the element and obtain the input focus. To do this, you must call the focus () method of the element. The problem arises: I don't want the viewmodel layer to know which interface elements need to get the input focus. To avoid the tight coupling between the view layer and the viewmodel layer, I need to use the messenger of mvvm light toolkit to remove strong references between them. First, create a new class movelogininputfocusmessage, which contains a string type attribute inputfieldname.
5. In loginviewmodel. CS, implement the two moveinputfocusto methods (message senders) involved in the pageloaded () method ).
6. In loginpage. XAML. CS, modify the loginpage constructor, register the message receiver, and provide the movelogininputfocus () method as the specific implementation.
Compiled.