WPF-MVVM计数器

简单的计数案例,ViewModel开启辅助线程,模拟后端运行,该方式可以扩展为项目中的后端线程数采及运算呈现,便于实时监控数据的场景。

代码如下:

MyCommand

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MVVMCountDemo.ViewModel
{
    public class MyCommand : ICommand
    {
        private readonly Action _execute;
        private readonly Func<bool> _canExecute;

        public MyCommand(Action execute, Func<bool> canExecute = null)
        {

            _execute = execute;
        }

        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 MVVMCountDemo.ViewModel
{
    public abstract class Notify : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

    }
}

Window.xaml

复制代码
<Window x:Class="MVVMCountDemo.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:MVVMCountDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label  FontSize="30" Content="{Binding Seconds}"   Foreground="Black" HorizontalAlignment="Left" Margin="141,85,0,0" VerticalAlignment="Top" Height="110" Width="230"/>
        <Button Content="开始/清零" HorizontalAlignment="Left" Margin="203,224,0,0" VerticalAlignment="Top" Width="100" Command="{Binding StartOrResetCommand}"/>
    </Grid>
</Window>

MainWindow.cs

复制代码
using MVVMCountDemo.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 MVVMCountDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new CountDemoViewModel();
        }
    }
}

CountDemoViewModel

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;

namespace MVVMCountDemo.ViewModel
{
    public class CountDemoViewModel:Notify
    {

        private DispatcherTimer timer;
        private int seconds = 0;

        public int Seconds
        {
            get { return seconds; }
            set
            {
                if (seconds != value)
                {
                    seconds = value;
                    OnPropertyChanged(nameof(Seconds));
                }
            }
        }


        public ICommand StartOrResetCommand { get; private set; }

        public CountDemoViewModel()
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += Timer_Tick;
            StartOrResetCommand = new MyCommand(StartOrReset);
        }

        int currentCount = 0;
        int lastCount = 0;

        private void Timer_Tick(object sender, EventArgs e)
        {
            currentCount++;
            Seconds = currentCount - lastCount;
        }




        private void StartOrReset()
        {
            if (timer.IsEnabled)
            {
                timer.Stop();
                Seconds = 0;
                lastCount = currentCount;
            }
            else
            {
                timer.Start();
            }
        }

    }
}
相关推荐
沉到海底去吧Go3 小时前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet5 小时前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf
冰茶_6 小时前
WPF之绑定模式深入
学习·microsoft·微软·c#·wpf·绑定模式
Vae_Mars8 小时前
WPF中如何自定义控件
wpf
冰茶_10 小时前
WPF之集合绑定深入
microsoft·微软·c#·wpf·mvvm·数据绑定·布局系统
凌霜残雪1 天前
WPF 3D图形编程核心技术解析
3d·wpf
△曉風殘月〆1 天前
WPF MVVM入门系列教程(五、命令和用户输入)
wpf·mvvm
△曉風殘月〆1 天前
WPF MVVM入门系列教程(六、ViewModel案例演示)
wpf·mvvm
凌霜残雪1 天前
深入解析WPF中的3D图形编程:材质与光照
3d·wpf·材质
△曉風殘月〆2 天前
WPF实时调试的一种实现方法
wpf