zoukankan      html  css  js  c++  java
  • C#WPF MVVM的简单实现

    INotifyPropertyChanged接口的实现

    internal class NotifyPropertyChanged : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    		
        	//属性变化时间通知
            protected void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    		//使用堆栈跟踪找寻属性名调用属性变化
            protected void RaisePropertyChanged()
            {
                var stack = new StackTrace();
                var lastFrame = stack.GetFrame(1);
                var methodName = lastFrame.GetMethod().Name;
                OnPropertyChanged(methodName);
            }
        }
    

    ICommand接口的简单实现

    public class RelayCommand : ICommand
        {
            // 通知命令能不能执行
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
    
            //判断命令能不能执行
            public bool CanExecute(object parameter)
            {
                return true;
            }
    
            //当命令执行的时候执行什么事情
            public void Execute(object parameter)
            {
                if (ExcutedAction == null)
                {
                    return;
                }
                ExcutedAction();
            }
        
        	//下面是类的构造函数和传进来命令执行时候调用的委托
            public Action ExcutedAction { get; set; }
    
            public Func<object,bool> CanExcutedFunc { get; set; }
    
            public RelayCommand(Action action)
            {
                ExcutedAction = action;
            }
        }
    
  • 相关阅读:
    多测师肖老师_git版本控制器之使用(3.2.3)
    多测师肖老师_linux之yum源解决方法(2.3)
    快速排序c++实现
    算法复杂性表示
    lua学习测试脚本
    获取程序当前文件夹 c#
    C#读写注册表 二进制写入
    [转]c# Config修改
    C# 文件版本信息读取
    lua中的table
  • 原文地址:https://www.cnblogs.com/niaofei123/p/15055876.html
Copyright © 2011-2022 走看看