项目日常应用,经常会使用到UserConrol来进行组合形成组合控件,组合控件在使用过程中,必然需要进行赋值,当前案例是UserControl中label的定时赋值。
1.分离关注点:MVVM 模式将应用程序分为三个主要部分,即模型(Model)、视图(View)和视图模型(ViewModel)。在这个例子中,MyUserControlViewModel
和 MainViewModel
充当 ViewModel,负责处理业务逻辑和数据操作;MyUserControl.xaml
和 MainWindow.xaml
是 View,负责呈现用户界面;而数据本身则相当于 Model。这种分离使得代码结构更加清晰,易于维护和测试。
2.属性更改通知:ViewModel 实现了 INotifyPropertyChanged
接口,当 ViewModel 中的属性值发生变化时,会触发 PropertyChanged
事件,通知视图更新相应的显示内容。这是 MVVM 模式中实现数据绑定自动更新的关键技术。
UserControl的xaml
UserControl的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 UserControlLabelsShow
{
///
/// UserControl1.xaml 的交互逻辑
///
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
DataContext = new UserControl1ViewModel();
}
}
}
UserControl1ViewModel
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UserControlLabelsShow
{
public class UserControl1ViewModel:Notify
{
private int _label1Value;
private int _label2Value;
private int _label3Value;
private int _label4Value;
private int _label5Value;
public int Label1Value
{
get { return _label1Value; }
set
{
if (_label1Value != value)
{
_label1Value = value;
OnPropertyChanged(nameof(Label1Value));
}
}
}
public int Label2Value
{
get { return _label2Value; }
set
{
if (_label2Value != value)
{
_label2Value = value;
OnPropertyChanged(nameof(Label2Value));
}
}
}
public int Label3Value
{
get { return _label3Value; }
set
{
if (_label3Value != value)
{
_label3Value = value;
OnPropertyChanged(nameof(Label3Value));
}
}
}
public int Label4Value
{
get { return _label4Value; }
set
{
if (_label4Value != value)
{
_label4Value = value;
OnPropertyChanged(nameof(Label4Value));
}
}
}
public int Label5Value
{
get { return _label5Value; }
set
{
if (_label5Value != value)
{
_label5Value = value;
OnPropertyChanged(nameof(Label5Value));
}
}
}
}
}
MainWindowViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace UserControlLabelsShow
{
public class MainWindowViewModel: Notify
{
public UserControl1ViewModel UserControl1ViewModel { get; set; }
public UserControl1ViewModel UserControl2ViewModel { get; set; }
public UserControl1ViewModel UserControl3ViewModel { get; set; }
public UserControl1ViewModel UserControl4ViewModel { get; set; }
private Timer _timer;
public MainWindowViewModel()
{
UserControl1ViewModel = new UserControl1ViewModel();
UserControl2ViewModel = new UserControl1ViewModel();
UserControl3ViewModel = new UserControl1ViewModel();
UserControl4ViewModel = new UserControl1ViewModel();
_timer = new Timer(TimerCallback, null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
}
private void TimerCallback(object state)
{
Application.Current.Dispatcher.Invoke(() =>
{
// 第一个 UserControl 赋值
UserControl1ViewModel.Label1Value++;
UserControl1ViewModel.Label2Value++;
UserControl1ViewModel.Label3Value++;
UserControl1ViewModel.Label4Value++;
UserControl1ViewModel.Label5Value++;
// 第二个 UserControl 赋值
UserControl2ViewModel.Label1Value = UserControl2ViewModel.Label1Value + 100;
UserControl2ViewModel.Label2Value = UserControl2ViewModel.Label2Value + 100;
UserControl2ViewModel.Label3Value = UserControl2ViewModel.Label3Value + 100;
UserControl2ViewModel.Label4Value = UserControl2ViewModel.Label4Value + 100;
UserControl2ViewModel.Label5Value = UserControl2ViewModel.Label5Value + 100;
// 第三个 UserControl 赋值
UserControl3ViewModel.Label1Value = UserControl3ViewModel.Label1Value + 200;
UserControl3ViewModel.Label2Value = UserControl3ViewModel.Label2Value + 200;
UserControl3ViewModel.Label3Value = UserControl3ViewModel.Label3Value + 200;
UserControl3ViewModel.Label4Value = UserControl3ViewModel.Label4Value + 200;
UserControl3ViewModel.Label5Value = UserControl3ViewModel.Label5Value + 200;
// 第四个 UserControl 赋值
UserControl4ViewModel.Label1Value = UserControl4ViewModel.Label1Value + 300;
UserControl4ViewModel.Label2Value = UserControl4ViewModel.Label2Value + 300;
UserControl4ViewModel.Label3Value = UserControl4ViewModel.Label3Value + 300;
UserControl4ViewModel.Label4Value = UserControl4ViewModel.Label4Value + 300;
UserControl4ViewModel.Label5Value = UserControl4ViewModel.Label5Value + 300;
});
}
}
}
MainWindow.xaml
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 UserControlLabelsShow
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
}
}
辅助类MyCommand
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace UserControlLabelsShow
{
public class MyCommand:ICommand
{
private readonly Action _execute;
private readonly Func _canExecute;
public MyCommand(Action execute, Func canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute();
}
public void Execute(object parameter)
{
_execute();
}
}
}
辅助类Notify
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace UserControlLabelsShow
{
public abstract class Notify : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}