wpf的MVVM框架(入门级)
作者:互联网
内容说明:视频教程总结
代码平台:visual studio 2019
内容简介:使用wpf的MVVM框架(view-model -viewmodel)实现简单数据绑定及行为绑定,实现前后端代码的分离; 实例为两个数相加,并显示计算结果。
1.view---MainWindow.xaml
<Window x:Class="MVVMdemo.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:MVVMdemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" ShowInTaskbar="True" WindowStartupLocation="CenterScreen" WindowState="Maximized" Topmost="True" >
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" Margin="158,84,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding Input1}"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="158,130,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding Input2}"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="158,178,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding Result}"/>
<Button Content="add" HorizontalAlignment="Left" Margin="337,130,0,0" VerticalAlignment="Top" Width="75" Command="{Binding AddCommand}" Height="23"/>
</Grid>
</Window>
2.MainWindow.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;
namespace MVVMdemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
}
}
3.NotificationObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows;
namespace MVVMdemo
{
class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
4.viewmodel---MainWindowViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MVVMdemo
{
class MainWindowViewModel:NotificationObject
{
//输入值1
private double input1;
public double Input1
{
get { return input1; }
set
{
input1 = value;
this.RaisePropertyChanged("Input1");
}
}
private double input2;
//输入值2
public double Input2
{
get { return input2; }
set
{
input2 = value;
this.RaisePropertyChanged("Input2");
}
}
//计算结果
private double result;
public double Result
{
get { return result; }
set
{
result = value;
this.RaisePropertyChanged("Result");
}
}
//
public DelegateCommand AddCommand { get; set; }
private void Add(object parameter)
{
//计算代码段
this.Result = this.Input1 + this.Input2;
}
public MainWindowViewModel()
{
this.AddCommand = new DelegateCommand();
this.AddCommand.ExecuteAction = new Action<object>(this.Add);
}
}
}
5.DelegateCommand.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace MVVMdemo
{
class DelegateCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
//if (CanExecuteFunc == null)
//{
//}
//this.CanExecuteFunc(parameter);
}
public void Execute(object parameter)
{
if (ExecuteAction == null)
{
return;
}
this.ExecuteAction(parameter);
}
public Action<object> ExecuteAction { get; set; }
public Func<object, bool> CanExecuteFunc { get; set; }
}
}
标签:set,MVVM,get,Windows,System,入门级,using,wpf,public 来源: https://blog.csdn.net/sxj1079080038/article/details/121447830