C#中Wpf常常会用到的两个基类

  • Post author:
  • Post category:其他


WPF(Windows Presentation Foundation)是微软推出的基于Windows 的用户界面框架,属于.NET Framework 3.0的一部分。它提供了统一的编程模型、语言和框架,真正做到了分离界面设计人员与开发人员的工作;同时它提供了全新的多媒体交互用户图形界面。

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp.BaseInfo
{
    public class NotifyBase : INotifyPropertyChanged
    {
        public void DoNotify([CallerMemberName] string propName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }

        public event PropertyChangedEventHandler? PropertyChanged;
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApp.BaseInfo
{
    public class BaseCommand : ICommand
    {
        public Action<object?>? ExecuteCommand { get; set; }

        public Func<object?, bool>? CanExecuteCommand { get; set; }

        public BaseCommand(Func<object?, bool>? canExecuteCommand)
        {
            CanExecuteCommand = canExecuteCommand;
        }

        public BaseCommand(Action<object?>? executeCommand)
        {
            ExecuteCommand = executeCommand;
        }

        public BaseCommand(Func<object?, bool> canExecuteCommand, Action<object?> executeCommand)
        {
            CanExecuteCommand = canExecuteCommand;
            ExecuteCommand = executeCommand;
        }

        public bool CanExecute(object? parameter)
        {
            return CanExecuteCommand?.Invoke(parameter) == true;
        }

        public void Execute(object? parameter)
        {
            ExecuteCommand?.Invoke(parameter);
        }

        public event EventHandler? CanExecuteChanged;
    }
}

两个类型用于,属性变化通知,和命令绑定。WPF中必不可少的东西。



版权声明:本文为weixin_43542114原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。