菲佣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

    }
}

 

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

posted @ 2013-02-16 14:20  qiurideyun  阅读(1543)  评论(0编辑  收藏  举报