WPF MVVM设计模式的ViewModelBase和CommandBase代码

ViewModelBase和CommandBase代码的整理如下:

 

1.ViewModelBase.cs

 

 

2.CommandBase.cs

 

关于WPF MVVM设计模式文档(Josh Smith),请阅读:http://msdn.microsoft.com/zh-cn/magazine/dd419663.aspx

此代码是Josh Smith的范例修改版本。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的WPF MVVM模式的登录界面及代码: XAML代码: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp1" Title="Login" Height="250" Width="350"> <Window.DataContext> <local:LoginViewModel /> </Window.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="Username:"/> <TextBox Grid.Row="0" Grid.Column="1" Width="200" Margin="5" Text="{Binding Username}" /> <Label Grid.Row="1" Grid.Column="0" Content="Password:"/> <PasswordBox Grid.Row="1" Grid.Column="1" Width="200" Margin="5" Password="{Binding Password}" /> <Button Grid.Row="2" Grid.Column="1" Width="100" Margin="5" Content="Login" Command="{Binding LoginCommand}" IsDefault="True"/> <Label Grid.Row="3" Grid.Column="1" Content="{Binding ErrorMessage}" Foreground="Red" HorizontalAlignment="Center"/> <CheckBox Grid.Row="4" Grid.Column="1" Content="Remember Me" Margin="5" IsChecked="{Binding RememberMe}"/> </Grid> </Window> ``` ViewModel代码: ```csharp using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Input; namespace WpfApp1 { public class LoginViewModel : INotifyPropertyChanged { private string _username; private string _password; private bool _rememberMe; private string _errorMessage; public string Username { get { return _username; } set { _username = value; OnPropertyChanged(); } } public string Password { get { return _password; } set { _password = value; OnPropertyChanged(); } } public bool RememberMe { get { return _rememberMe; } set { _rememberMe = value; OnPropertyChanged(); } } public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; OnPropertyChanged(); } } public ICommand LoginCommand { get; set; } public LoginViewModel() { LoginCommand = new RelayCommand(Login, CanLogin); } private bool CanLogin() { return !string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password); } private void Login() { if (Username == "admin" && Password == "password") { if (RememberMe) { // Save login information to file or database } MessageBox.Show("Login successful!"); } else { ErrorMessage = "Invalid username or password"; } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } ``` 其中,`RelayCommand`是一个实现了`ICommand`接口的自定义命令类,用于绑定按钮的点击事件。你可以在网上找到相关实现代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值