• 用开源AOP简化MVVM框架


    本文的前提是知晓基于Xaml开发,本文以WPF为例

    一 、简化属性通知事件

    普通的属性通知会写一个基于INotifyPropertyChanged接口的类

     1 public class RasiePropertyChanged : INotifyPropertyChanged
     2     {
     3         public event PropertyChangedEventHandler PropertyChanged;
     4 
     5         protected void OnPropertyChanged([CallerMemberName]string propertyName = null)
     6         {
     7             PropertyChangedEventHandler handler = PropertyChanged;
     8             if (handler != null)
     9             {
    10                 handler(this, new PropertyChangedEventArgs(propertyName));
    11             }
    12         }
    13 
    14     }

    这样用时就可以在属性的Set里最后加上一句RasiePropertyChanged();就可以,但是如果属性只是简单的Get,Set写起来也是比较麻烦的

    使用Fody/PropertyChanged可省去此麻烦

    项目地址:https://github.com/Fody/PropertyChanged

    使用方式如下,转自官方

    [ImplementPropertyChanged]
    public class Person 
    {        
        public string GivenNames { get; set; }
        public string FamilyName { get; set; }
    
        public string FullName
        {
            get
            {
                return string.Format("{0} {1}", GivenNames, FamilyName);
            }
        }
    }

    在类上边写上一个Attribute [ImplementPropertyChanged],类里的所有属性就都实现了属性通知事件

    DoNotNotify:如果有某个属性不想实现通知事件,就在相应属性上加个[DoNotNotify]

    AlsoNotifyFor:如果有某个属性像上边的FullName一样是2个属性的组合,任何一个变化都要通知都FullName变化,就在子属性GivenNames 和FamilyName上加个[AlsoNotifyFor("FullName")]

    DependsOn:如果反过来FullName变了也让子属性变化,那就要在FullName上加上[DependsOn("GivenName","FamilyName")]

    DoNotSetChanged:这个属性是说当FullName 的Set改变时,不会通知到子属性

    DoNotCheckEquality:这个属性是说跳过相等的检查,没有实例,我也没有用过

    二、简化ICommand的绑定事件

    如果绑定一个Button 的点击事件,正常的后台是写一个DeletedCommand的属性

     1 private ICommand _clickCommand;
     2 
     3         public ICommand ClickCommand
     4         {
     5             get { return _clickCommand ?? new DelegateCommand(Click); }
     6             set { _clickCommand = value; }
     7         }
     8 
     9         10         private Action Click()
    11         {
    12             throw new NotImplementedException();
    13         }

    然后前台绑定这个ClickCommand
    使用Fody/Commander.Fody可省去写ICommand的属性

    项目地址:https://github.com/DamianReeves/Commander.Fody

    使用方式如下,转自官方

    1 [OnCommand("ClickCommand")]
    2         private Action Click()
    3         {
    4             throw new NotImplementedException();
    5         }

    如此就可以了
    但是ICommand接口有2个方法,一个是Execute,一个是CanExecute

    所以属性自然也是有2个,分别对应这2个方法OnCommandOnCommandCanExecute

    如有问题请参照项目说明和示例,本人只是恰巧看到了这2个简单的Fody的项目,简单用一下而已

  • 相关阅读:
    用csc命令行手动编译cs文件
    笔录---果壳中的C#第一章
    Visual Studio2012快捷键总结
    JavaScript 二维数组排列组合2
    JavaScript 递归法排列组合二维数组2
    JavaScript 递归法排列组合二维数组
    JavaScript 二维数组排列组合
    在 CentOS6 上安装 GraphicsMagick-1.3.30
    Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.6:resources failed: Unable to load the mojo 'resources' (or one of its required components)
    java.sql.SQLException: Column count doesn't match value count at row 1
  • 原文地址:https://www.cnblogs.com/heyixiaoran/p/5095679.html
Copyright © 2020-2023  润新知