• 菲佣WPF——6(NotifyObject CallMemberName重写)


    再这里感谢john_masen同学。

    Lambda Expression还是比较繁琐。使用CallMemberName重写 NotifyObject

    NotifyObject代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ComponentModel;
    using System.Linq.Expressions;
    using System.Runtime.CompilerServices;
    
    namespace WpfApplication4
    {
        public abstract class NotifyObject:INotifyPropertyChanged
        {
            #region << Field >>
            private Dictionary<string, object> cache = new Dictionary<string, object>();
            #endregion
    
            #region << Property >>
            public event PropertyChangedEventHandler PropertyChanged;
            #endregion
    
            #region << Method >>
            //public T GET<T>(Expression<Func<T>> express)
            //{
            //    return GetPropertyValue<T>(GetPropertyName<T>(express));
            //}
    
            //public void SET<T>(Expression<Func<T>> express, object obj)
            //{
            //    SetPropertyValue(GetPropertyName<T>(express), obj);
            //}
    
            //private string GetPropertyName<T>(Expression<Func<T>> express)
            //{
            //    var memExpress = (MemberExpression)express.Body;
    
            //    if (memExpress == null)
            //        throw new Exception("The expression is valid");
    
            //    return memExpress.Member.Name;
            //}
    
            private T GetPropertyValue<T>(string propertyName)
            {
                if (string.IsNullOrEmpty(propertyName))
                    return default(T);
                else
                {
                    if (cache.ContainsKey(propertyName))
                        return (T)cache[propertyName];
                    else
                        return default(T);
                }
            }
            public T GET<T>([CallerMemberName] string propertyName = null)
            {
                return GetPropertyValue<T>(propertyName);
            }
    
            public void SET<T>(T obj, [CallerMemberName] string propertyName = null)
            {
                SetPropertyValue(propertyName, obj);
            }
    
            private void SetPropertyValue(string propertyName, object obj)
            {
                if (cache.ContainsKey(propertyName))
                {
                    if (!Object.ReferenceEquals(cache[propertyName], obj))
                    {
                        cache[propertyName] = obj;
                        Notify(propertyName);
                    }
                }
                else
                {
                    cache.Add(propertyName, obj);
                    Notify(propertyName);
                }
            }
    
            private void Notify(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
            #endregion
        }
    }

    ViewModel属性的设置:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    
    namespace WpfApplication4
    {
        public class MainWindowsViewModel:NotifyObject
        {
            #region << Property >>
            public bool IsVisible
            {
                get { return GET<bool>(); }
                set { SET<bool>(value); }
            }
    
            public string Name
            {
                get { return GET<string>(); }
                set { SET<string>(value); }
            }
    
            public ICommand ClickCommand { get; set; }
            #endregion
            #region << Constructor >>
            public MainWindowsViewModel()
            {
                IsVisible = true;
                ClickCommand = new DeletegateCommand(Click);
            }
            #endregion
    
            #region << Method >>
            public void Click()
            {
                Name = "Click";              
            }
            #endregion
    
        }
    }

    在这里希望大家给出好的建议,大家一起进步

  • 相关阅读:
    SQL批量更新
    使用PLSQL导入导出数据库
    Oracle 的Blob使用小结
    基于java的邮件服务器以及webmail的搭建
    Tomcat 系统架构与设计模式 【2】
    修改Oracle XE Listener 占用的1521、8080端口
    nls_lang pl/sql 设置编码
    oracle提高查询效率的解析
    Tomcat 系统架构与设计模式
    hql与sql的区别(转)
  • 原文地址:https://www.cnblogs.com/qiurideyun/p/2913365.html
Copyright © 2020-2023  润新知