MVVM(Model-View-ViewModel) 是一种设计模式,用于分离用户界面(View)与业务逻辑(ViewModel)和数据模型(Model)。这种模式特别适用于 WPF(Windows Presentation Foundation)应用程序,因为它可以充分利用 WPF 的数据绑定、命令和依赖属性等特性,提高应用程序的可维护性和可测试性。
- MVVM 的三个主要组件
Model(模型):
定义:表示应用程序的数据模型,通常是一些类,这些类包含业务逻辑和数据访问代码。
职责:处理数据的获取、存储和验证等操作。
示例
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
View(视图):
定义:表示用户界面,通常是一个 XAML 文件,包含 UI 控件和布局。
职责:显示数据和处理用户输入,但不包含任何业务逻辑。
示例:
<Window x:Class="Wpfxfdemo.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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:Wpfxfdemo"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Text="{Binding UserName}" Margin="10" Grid.Column="0" ></TextBox>
<Button Content="Save" Command="{Binding SaveCommand}" Margin="10" Grid.Column="1"></Button>
<Button Content="Open" Command="{Binding OpenWindows}" Grid.Row="1"/>
</Grid>
</Window>
ViewModel(视图模型):
定义:充当 Model 和 View 之间的中介,包含 UI 逻辑和业务逻辑。
职责:处理数据绑定、命令绑定和用户交互逻辑。
示例:
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;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
namespace Wpfxfdemo
{
public class MainViewModel: INotifyPropertyChanged
{
private string _userName;
public string UserName
{
get => _userName;
set
{
if (_userName != value)
{
_userName = value;
OnPropertyChanged(nameof(UserName));
}
}
}
public ICommand SaveCommand { get; private set; }
public ICommand OpenWindows { get; set; }
public MainViewModel()
{
SaveCommand = new RelayCommand(Save);
OpenWindows = new RelayCommand(OpenCommand);
}
private void Save()
{
var ss = UserName;
UserName ="333";
}
private void OpenCommand()
{
Window1 wid = new Window1();
wid.Show();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
页面(xaml)与ViewModel绑定
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 Wpfxfdemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext =new MainViewModel();
}
}
}
标签:set,MVVM,get,Windows,System,MvvmLight,using,WPF,public
From: https://www.cnblogs.com/ailonchen/p/18672945