1. 程式人生 > >Wpf例項-MVVM模式實現的登入窗體

Wpf例項-MVVM模式實現的登入窗體

  1. 程式截圖

程式截圖

2.程式的結構圖

程式的結構圖

3.說明:
如圖2所示,Model中存放的是User類檔案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WpfApp.Model
{
   public class User
    {
       public string UserName { get; set; }
       public int UserId { get; set; }
       public string Password { get
; set; } } }

ViewModel

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using WpfApp.Model;
using WpfApp.Command;
using System.Windows;


namespace WpfApp.ViewModel
{
    public
class UserViewModel : INotifyPropertyChanged { #region 私有欄位 private string userName; private string userPassword; private ICommand loginCommand; private User user = new User(); #endregion #region 屬性 public ICommand LoginCommand { get
{ return loginCommand; } } public string UserName { get { return userName; } set { this.userName = value; if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs("UserName")); } } } public string UserPassword { get { return userPassword; } set { this.userPassword = value; if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs("UserPassword")); } } } #endregion public UserViewModel() { user.UserId = 1; user.UserName = "huangxi"; user.Password = "123"; loginCommand = new LoginCommand(this); } #region 公共方法 public void QueryData() { if (!string.IsNullOrEmpty(this.UserName)) { if (this.UserName != user.UserName) { return; } if (this.UserPassword != user.Password) { return; } MessageBox.Show("登入成功"); } } #endregion #region INotifyPropertyChanged成員 public event PropertyChangedEventHandler PropertyChanged; #endregion } }

還有Command,存放的是Command,實現了ICommand介面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using WpfApp.ViewModel;

namespace WpfApp.Command
{
    public class LoginCommand:ICommand
    {
        private UserViewModel userViewModel;
        #region ICommand成員
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            userViewModel.QueryData();
        } 
        #endregion

        public LoginCommand(UserViewModel userViewModel)
        {
            this.userViewModel = userViewModel;
        }
    }
}

最後是View層,

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label Content="使用者名稱" HorizontalAlignment="Left" Margin="73,80,0,0" VerticalAlignment="Top"/>
        <Label Content="密碼" HorizontalAlignment="Left" Margin="73,138,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.798,-0.492"/>
        <Button Content="BtnLogin" HorizontalAlignment="Left" Margin="229,221,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <TextBox  x:Name ="txtUserName" HorizontalAlignment="Left" Height="23" Margin="203,80,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="120" Text="{Binding UserName,Mode=TwoWay}"/>

        <TextBox x:Name="txtPassword" HorizontalAlignment="Left" Height="23" Margin="203,141,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding UserPassword,Mode=TwoWay}"/>

    </Grid>
</Window>

這裡有一個問題,因為預設的Password沒法繫結,這裡採用的TextBox代替,網路上有很多Password的幫助類,有興趣的同學可以自行百度。

介面層的後臺程式碼

using System.Windows;
using WpfApp.ViewModel;

namespace WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        private UserViewModel userViewModel = null;
        public MainWindow()
        {
            InitializeComponent();
            userViewModel = new UserViewModel();
            this.DataContext = userViewModel;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            if (this.userViewModel != null)
            {
                this.userViewModel.UserName = this.txtUserName.Text;
                this.userViewModel.UserPassword = this.txtPassword.Text;
                this.userViewModel.LoginCommand.Execute(null);
            }
        }
    }
}

到這裡就結束了。