WPF MVVM Mastering 2: Implement a login window

What we need to do a kind of thing? Directly on the map:

It looks simple, but to finish the login window, the entry of MVVM basically completed. (Why the login screen to choose the sex so strange? Simply because RadioButton Binding is a problem)

Many tutorials are a small example, people just getting started with the time do not know how to use in the project. I am here to speak briefly from the perspective of a development project.

First of all, the window is just one of many a project window. For simplicity, we put the project file as follows:

We created a new ViewModel folder, which is divided by file View folder, then each folder which contains the Model class and ViewModel classes. Meanwhile, there is a folder ViewModel Common folder, store some of the need to share ViewModel class. Of course, if readers have a better idea, absolutely no need to do it according to this model.

Project started, we do not hurry to write code, but look at what View which contains data. I have listed a table:

Chinese name Types of English name
username string UserName
password string Password
gender int Gender
Window initialization View->ViewModel WndInit
Event Log View->ViewModel LoginClick
Close behavior ViewModel->View ToClose
Open a new window ViewModel->View OpenWnd

The first three are obvious, the back four possible we do not think is a data. But in the MVVM pattern, narrow data, events, actions have become an element that can be bound, it can be said to be data.

We mentioned earlier, when the View and ViewModel change state, are broadcast in a similar way to do it. They do not pass objects, but only pass a name. So, in order to allow programmers and artists to act separately, in terms of naming, we should start fixed.

Now, we can begin to develop the Model layer. Model layer codes as follows:

  1. namespace LoginDemo.ViewModel.Login
  2. {
  3. /// <summary>
  4. /// login window Model
  5. /// </summary>
  6. public class LoginModel
  7. {
  8. /// <summary>
  9. /// 用户名
  10. /// </summary>
  11. public string UserName { get; set; }
  12.  
  13. /// <summary>
  14. /// 密码
  15. /// </summary>
  16. public string Password { get; set; }
  17.  
  18. /// <summary>
  19. /// 性别
  20. /// </summary>
  21. public int Gender { get; set; }
  22. }
  23. }

Model层的代码就是这样,非常单纯,也没有什么新的知识。虽然我们后面会实现各种交互的逻辑,但Model层的代码已经不会改变了。

Guess you like

Origin www.cnblogs.com/ljdong7/p/12040159.html