五、炫饭馆项目实战
需求分析:设计一个这样的页面
一、新建WPF .NetFramework应用
以LoginFrame解决方案为例
二、页面布局设计
Grid共四行(第一行Height为auto,第二行Height为auto,第三行Height为1*,第四行Height为9*)
①第一行是TextBlock(<TextBlock Grid.Row="0" Text="淦饭馆" FontSize="30" HorizontalAlignment="Center" Margin="5"/>
)
②第二行是StackPanel布局里放TextBlock(因为需要把整个背景颜色填充故需要StackPanel布局)
<StackPanel Grid.Row="1" Background="Blue"><TextBlock Text="Login" FontSize="22" HorizontalAlignment="Center" Foreground="White" Margin="5"/>
</StackPanel>
③第三行单纯为了占位,布局好看而已
④第四行是Grid布局(水平居中),共4行2列(四行Height均为30,第一列Width为auto,第二列Width为200)
其中第一行存放TextBlock和TextBox,账号和输入框
<TextBlock Grid.Row="0" Grid.Column="0" Text="账号:" VerticalAlignment="Center"/>
<TextBox Grid.Row="0" Grid.Column="1" Margin="2"/>
第二行也存放TextBlock和TextBox,密码和输入框
<TextBlock Grid.Row="1" Grid.Column="0" Text="账号:" VerticalAlignment="Center"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="2"/>
第三行存放CheckBox,通过Grid.ColumnSpan合并单元格,记住密码
<CheckBox Grid.Row="2" Content="记住密码" Grid.ColumnSpan="2"/>
第四行存放Button,也是通过Grid.ColumnSpan合并单元格,登录按钮
<Button Grid.Row="3" Content="登录" Grid.ColumnSpan="2"/>
<Grid><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition Height="auto"/><RowDefinition Height="1*"/><RowDefinition Height="9*"/></Grid.RowDefinitions><TextBlock Grid.Row="0" Text="惠泽小大王炫饭馆" FontSize="30" HorizontalAlignment="Center" Margin="5"/><StackPanel Grid.Row="1" Background="Blue"><TextBlock Text="Login" FontSize="22" HorizontalAlignment="Center" Foreground="White" Margin="5"/></StackPanel><Grid Grid.Row="3" HorizontalAlignment="Center"><Grid.RowDefinitions><RowDefinition Height="30"/><RowDefinition Height="30"/><RowDefinition Height="30"/><RowDefinition Height="30"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="auto"/><ColumnDefinition Width="200"/></Grid.ColumnDefinitions><TextBlock Grid.Row="0" Grid.Column="0" Text="账号:" VerticalAlignment="Center"/><TextBox Grid.Row="0" Grid.Column="1" Margin="2"/><TextBlock Grid.Row="1" Grid.Column="0" Text="账号:" VerticalAlignment="Center"/><TextBox Grid.Row="1" Grid.Column="1" Margin="2"/><CheckBox Grid.Row="2" Content="记住密码" Grid.ColumnSpan="2"/><Button Grid.Row="3" Content="登录" Grid.ColumnSpan="2"/></Grid>
</Grid>
主页面
Hzxdw_mainwindow.xaml
<Window x:Class="LoginFrame.Hzxdw_mainwindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:LoginFrame"mc:Ignorable="d"Title="Hzxdw_mainwindow" Height="450" Width="800"><Grid><ListView><ListViewItem Content="爆炒凉皮 大8 小6"/><ListViewItem Content="炒酸奶 大7 小5"/><ListViewItem Content="秘制鸡爪 大5 小3"/><ListViewItem Content="新疆炒粉 大12 小8"/></ListView></Grid>
</Window>
Hzxdw_mainwindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;namespace LoginFrame
{/// <summary>/// Hzxdw_mainwindow.xaml 的交互逻辑/// </summary>public partial class Hzxdw_mainwindow : Window{public Hzxdw_mainwindow(){InitializeComponent();this.Title = "欢迎光临惠泽小大王炫饭馆";}}
}
三、页面功能设计
Ⅰ,使用x:Name进行xmal和C#交互
1,给Button加上Click事件<Button Grid.Row="3" Content="登录" Grid.ColumnSpan="2" Click="Button_Click"/>
给账号和密码输入框添加x:Name
<TextBox Grid.Row="0" Grid.Column="1" Margin="2" x:Name="txt_username"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="2" x:Name="txt_password"/>
2,新建一个WPF窗口,作为登录成功之后的主页面
3,先写死账号和密码,登录成功跳转到Hzxdw_mainwindows,xaml页面;登录失败清空账号和密码
<Window x:Class="LoginFrame.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:LoginFrame"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition Height="auto"/><RowDefinition Height="1*"/><RowDefinition Height="9*"/></Grid.RowDefinitions><TextBlock Grid.Row="0" Text="惠泽小大王炫饭馆" FontSize="30" HorizontalAlignment="Center" Margin="5"/><StackPanel Grid.Row="1" Background="Blue"><TextBlock Text="Login" FontSize="22" HorizontalAlignment="Center" Foreground="White" Margin="5"/></StackPanel><Grid Grid.Row="3" HorizontalAlignment="Center"><Grid.RowDefinitions><RowDefinition Height="30"/><RowDefinition Height="30"/><RowDefinition Height="30"/><RowDefinition Height="30"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="auto"/><ColumnDefinition Width="200"/></Grid.ColumnDefinitions><TextBlock Grid.Row="0" Grid.Column="0" Text="账号:" VerticalAlignment="Center"/><TextBox Grid.Row="0" Grid.Column="1" Margin="2" x:Name="txt_username"/><TextBlock Grid.Row="1" Grid.Column="0" Text="账号:" VerticalAlignment="Center"/><TextBox Grid.Row="1" Grid.Column="1" Margin="2" x:Name="txt_password"/><CheckBox Grid.Row="2" Content="记住密码" Grid.ColumnSpan="2"/><Button Grid.Row="3" Content="登录" Grid.ColumnSpan="2" Click="Button_Click"/></Grid></Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace LoginFrame
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void Button_Click(object sender, RoutedEventArgs e){string username = txt_username.Text;string password = txt_password.Text;if (username == "HZXDW" && password == "666") {Hzxdw_mainwindow hzxdw_mainwindow = new Hzxdw_mainwindow();hzxdw_mainwindow.Show();this.Hide();}else {MessageBox.Show("账号或密码输入有误!");txt_username.Text = "";txt_password.Text = "";}}}
}
输入错误账号和密码,错误弹窗
输入正确的账号和密码
成功进入主页面
Ⅱ,使用Binding进行xaml和C#交互
我们可以看到使用x:Name
进行前后端交互的时候很难受,因为我们要知道x:Name到底是个啥,而且还需要获取赋值之类的操作
解决方法:在xaml中通过Binding进行绑定一个变量,C#直接用就可以了,该变量就是所绑定的控件属性
在MainWindow.xaml中对账号和密码文本框绑定变量(应该说是函数)
<TextBox Grid.Row="0" Grid.Column="1" Margin="2" Text="{Binding txt_name}"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="2" Text="{Binding txt_pwd}"/>
在MainWindow.xaml.cs中声明变量(具体应该来说是函数)
private string txt_name_;
public string txt_name
{get { return txt_name_; }set{txt_name_ = value;RaisePropertyChanged(txt_name);}
}
private string txt_pwd_;public string txt_pwd{get { return txt_pwd_; }set{txt_pwd_ = value;RaisePropertyChanged(txt_pwd);}}
MainWindow类需要继承INotifyPropertyChanged父类(为了将属性通知到界面中去,若不继承,则实际的修改后的结果不会实时更新在界面上),
public partial class MainWindow : Window, INotifyPropertyChanged//继承该接口
重写该接口
public event PropertyChangedEventHandler PropertyChanged;//重写该接口
private void RaisePropertyChanged(string propertyName)//重写该接口
{PropertyChangedEventHandler handler = PropertyChanged;if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
在构造函数里面指定上下文this.DataContext = this;
此时C#里面的txt_name变量就是xaml中的关于账号的TextBox的Text属性
C#里面的txt_pwd变量就是xaml中的关于密码的TextBox的Text属性
<Window x:Class="LoginFrame.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:LoginFrame"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition Height="auto"/><RowDefinition Height="1*"/><RowDefinition Height="9*"/></Grid.RowDefinitions><TextBlock Grid.Row="0" Text="惠泽小大王炫饭馆" FontSize="30" HorizontalAlignment="Center" Margin="5"/><StackPanel Grid.Row="1" Background="Blue"><TextBlock Text="Login" FontSize="22" HorizontalAlignment="Center" Foreground="White" Margin="5"/></StackPanel><Grid Grid.Row="3" HorizontalAlignment="Center"><Grid.RowDefinitions><RowDefinition Height="30"/><RowDefinition Height="30"/><RowDefinition Height="30"/><RowDefinition Height="30"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="auto"/><ColumnDefinition Width="200"/></Grid.ColumnDefinitions><TextBlock Grid.Row="0" Grid.Column="0" Text="账号:" VerticalAlignment="Center"/><TextBox Grid.Row="0" Grid.Column="1" Margin="2" Text="{Binding txt_name}"/><TextBlock Grid.Row="1" Grid.Column="0" Text="账号:" VerticalAlignment="Center"/><TextBox Grid.Row="1" Grid.Column="1" Margin="2" Text="{Binding txt_pwd}"/><CheckBox Grid.Row="2" Content="记住密码" Grid.ColumnSpan="2"/><Button Grid.Row="3" Content="登录" Grid.ColumnSpan="2" Click="Button_Click"/></Grid></Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;namespace LoginFrame
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window, INotifyPropertyChanged//继承该接口{public MainWindow(){InitializeComponent();this.DataContext = this;//声明属性变量使用的是本窗口下的属性,指定上下文,可以单独指定,也可以全部指定}public event PropertyChangedEventHandler PropertyChanged;//重写该接口private void RaisePropertyChanged(string propertyName)//重写该接口{PropertyChangedEventHandler handler = PropertyChanged;if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));}//输入propfull 按两次tab可快速创建//Ctrl + K + Ctrl + D 格式化private string txt_name_;public string txt_name{get { return txt_name_; }set{txt_name_ = value;RaisePropertyChanged(txt_name);}}private string txt_pwd_;public string txt_pwd{get { return txt_pwd_; }set{txt_pwd_ = value;RaisePropertyChanged(txt_pwd);}}private void Button_Click(object sender, RoutedEventArgs e){if (txt_name == "HZXDW" && txt_pwd == "666"){Hzxdw_mainwindow hzxdw_mainwindow = new Hzxdw_mainwindow();hzxdw_mainwindow.Show();this.Hide();}else{MessageBox.Show("账号或密码输入有误!");txt_name = "";txt_pwd = "";}}}
}
账号密码错误
自动清除
账号密码正确
进入主页面
Ⅲ,使用MVVM数据模型进行xaml和C#交互(文本框属性&按钮点击事件)
前面的Ⅰ和Ⅱ我们也都看到了有很多的缺点,我们的目的就是尽可能的解耦合,前后端分离,互不干扰最好
1,文本框属性绑定
代码 --- MVVM --- 界面
,MVVM起到桥梁的作用
MVVM——M(Model模型)、V(View界面)、VM(ViewModel整合业务)
新建两个类(LoginModel和LoginViewModel)
MainWindow.xaml
<Window x:Class="LoginFrame.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:LoginFrame"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition Height="auto"/><RowDefinition Height="1*"/><RowDefinition Height="9*"/></Grid.RowDefinitions><TextBlock Grid.Row="0" Text="惠泽小大王炫饭馆" FontSize="30" HorizontalAlignment="Center" Margin="5"/><StackPanel Grid.Row="1" Background="Blue"><TextBlock Text="Login" FontSize="22" HorizontalAlignment="Center" Foreground="White" Margin="5"/></StackPanel><Grid Grid.Row="3" HorizontalAlignment="Center"><Grid.RowDefinitions><RowDefinition Height="30"/><RowDefinition Height="30"/><RowDefinition Height="30"/><RowDefinition Height="30"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="auto"/><ColumnDefinition Width="200"/></Grid.ColumnDefinitions><TextBlock Grid.Row="0" Grid.Column="0" Text="账号:" VerticalAlignment="Center"/><TextBox Grid.Row="0" Grid.Column="1" Margin="2" Text="{Binding viewmodel_username}"/><TextBlock Grid.Row="1" Grid.Column="0" Text="账号:" VerticalAlignment="Center"/><TextBox Grid.Row="1" Grid.Column="1" Margin="2" Text="{Binding viewmodel_password}"/><CheckBox Grid.Row="2" Content="记住密码" Grid.ColumnSpan="2"/><Button Grid.Row="3" Content="登录" Grid.ColumnSpan="2" Click="Button_Click"/></Grid></Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;namespace LoginFrame
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{LoginViewModel loginViewModel = new LoginViewModel();public MainWindow(){InitializeComponent();this.DataContext = loginViewModel;//指定上下文,可以单独指定,也可以全部指定//loginViewModel.viewmodel_username = "YY"; //也可以为文本框进行初始化//loginViewModel.viewmodel_password = "1005";}private void Button_Click(object sender, RoutedEventArgs e){if (loginViewModel.viewmodel_username == "HZXDW" && loginViewModel.viewmodel_password == "666"){Hzxdw_mainwindow hzxdw_mainwindow = new Hzxdw_mainwindow();hzxdw_mainwindow.Show();this.Hide();}else{MessageBox.Show("账号或密码输入有误!");loginViewModel.viewmodel_username = "";loginViewModel.viewmodel_password = "";}}}
}
LoginViewModel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace LoginFrame
{public class LoginViewModel:INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;//重写该接口private void RaisePropertyChanged(string propertyName)//重写该接口{PropertyChangedEventHandler handler = PropertyChanged;if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));}private LoginModel login_model_ = new LoginModel();public string viewmodel_username{get { return login_model_.model_username; }set { login_model_.model_username = value;RaisePropertyChanged(viewmodel_username);}}public string viewmodel_password{get { return login_model_.model_password; }set {login_model_.model_password = value;RaisePropertyChanged(viewmodel_password);}}}
}
LoginModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace LoginFrame
{public class LoginModel{private string model_username_;public string model_username{get { return model_username_; }set { model_username_ = value; }}private string model_password_;public string model_password{get { return model_password_; }set { model_password_ = value; }}}
}
账号或密码错误,点击确定自动清除文本框
密码正确,进入主页面
2,按钮点击事件绑定
文本框的Text属性可以和后端进行绑定,接下来我们搞一下Button的点击事件也进行相关的交互操作
事件的交互绑定需要对应的类去继承ICommand,所在的命名空间为using System.Windows.Input;
新建一个类RelayCommond
RelayCommond.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;namespace LoginFrame
{public class RelayCommond:ICommand{readonly Func<bool> _canExecute;//命令是否能够执行readonly Action _execute;//命令需要执行的方法public RelayCommond(Action action,Func<bool> canExecute) {_execute = action;_canExecute = canExecute;}public bool CanExecute(object parameter) {if (_canExecute == null) {return true;}return _canExecute();}public void Execute(object parameter) {_execute();}public event EventHandler CanExecuteChanged {add {if (_canExecute != null) {CommandManager.RequerySuggested += value;}}remove {if (_canExecute != null) {CommandManager.RequerySuggested -= value;}}}}
}
LoginModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace LoginFrame
{public class LoginModel{private string model_username_;public string model_username{get { return model_username_; }set { model_username_ = value; }}private string model_password_;public string model_password{get { return model_password_; }set { model_password_ = value; }}}
}
LoginViewModel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;namespace LoginFrame
{public class LoginViewModel:INotifyPropertyChanged{private MainWindow mainwindow_;//ctor 回车,自动生成构造函数public LoginViewModel(MainWindow mainwindow){mainwindow_ = mainwindow;}public event PropertyChangedEventHandler PropertyChanged;//重写该接口private void RaisePropertyChanged(string propertyName)//重写该接口{PropertyChangedEventHandler handler = PropertyChanged;if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));}private LoginModel login_model_ = new LoginModel();public string viewmodel_username{get { return login_model_.model_username; }set { login_model_.model_username = value;RaisePropertyChanged(viewmodel_username);}}public string viewmodel_password{get { return login_model_.model_password; }set {login_model_.model_password = value;RaisePropertyChanged(viewmodel_password);}}void LoginFunc() {if (viewmodel_username == "HZXDW" && viewmodel_password == "666"){Hzxdw_mainwindow hzxdw_mainwindow = new Hzxdw_mainwindow();hzxdw_mainwindow.Show();mainwindow_.Hide();}else{MessageBox.Show("账号或密码输入有误!");viewmodel_username = "";viewmodel_password = "";}}// 目前就是为了返回一个bool值,仅此而已bool CanLoginExecute() { return true; }public ICommand LoginAction{get {//return new RelayCommond(LoginFunc, () => { return true; });//二选一 都可以return new RelayCommond(LoginFunc, CanLoginExecute); }}}
}
MainWindow.xaml
<Window x:Class="LoginFrame.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:LoginFrame"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition Height="auto"/><RowDefinition Height="1*"/><RowDefinition Height="9*"/></Grid.RowDefinitions><TextBlock Grid.Row="0" Text="惠泽小大王炫饭馆" FontSize="30" HorizontalAlignment="Center" Margin="5"/><StackPanel Grid.Row="1" Background="Blue"><TextBlock Text="Login" FontSize="22" HorizontalAlignment="Center" Foreground="White" Margin="5"/></StackPanel><Grid Grid.Row="3" HorizontalAlignment="Center"><Grid.RowDefinitions><RowDefinition Height="30"/><RowDefinition Height="30"/><RowDefinition Height="30"/><RowDefinition Height="30"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="auto"/><ColumnDefinition Width="200"/></Grid.ColumnDefinitions><TextBlock Grid.Row="0" Grid.Column="0" Text="账号:" VerticalAlignment="Center"/><TextBox Grid.Row="0" Grid.Column="1" Margin="2" Text="{Binding viewmodel_username}"/><TextBlock Grid.Row="1" Grid.Column="0" Text="账号:" VerticalAlignment="Center"/><TextBox Grid.Row="1" Grid.Column="1" Margin="2" Text="{Binding viewmodel_password}"/><CheckBox Grid.Row="2" Content="记住密码" Grid.ColumnSpan="2"/><Button Grid.Row="3" Content="登录" Grid.ColumnSpan="2" Command="{Binding LoginAction}"/></Grid></Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;namespace LoginFrame
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();LoginViewModel loginViewModel = new LoginViewModel(this);this.DataContext = loginViewModel;//指定上下文,可以单独指定,也可以全部指定}}
}
这样看起来MainWindow.xaml.cs里面的内容就很少很舒服了