BindableBase.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. using System;
  15. using System.Collections.Generic;
  16. using System.ComponentModel;
  17. using System.Linq;
  18. using System.Runtime.CompilerServices;
  19. using System.Text;
  20. using Senparc.Weixin.Annotations;
  21. namespace Senparc.Weixin.Entities
  22. {
  23. /// <summary>
  24. /// 用于实现INotifyPropertyChanged
  25. /// </summary>
  26. [Serializable]
  27. public abstract class BindableBase : INotifyPropertyChanged
  28. {
  29. public event PropertyChangedEventHandler PropertyChanged;
  30. [NotifyPropertyChangedInvocator]
  31. #if NET35 || NET40
  32. protected virtual void OnPropertyChanged(string propertyName)
  33. #else
  34. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  35. #endif
  36. {
  37. var eventHandler = this.PropertyChanged;
  38. if (eventHandler != null)
  39. {
  40. eventHandler(this, new PropertyChangedEventArgs(propertyName));
  41. }
  42. //PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));//需要VS2015+或新编译器支持
  43. }
  44. /// <summary>
  45. /// 设置属性
  46. /// </summary>
  47. /// <typeparam name="T"></typeparam>
  48. /// <param name="storage"></param>
  49. /// <param name="value"></param>
  50. /// <param name="propertyName"></param>
  51. /// <returns></returns>
  52. /// #if NET35 || NET40
  53. #if NET35 || NET40
  54. protected bool SetProperty<T>(ref T storage, T value, String propertyName)
  55. #else
  56. protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
  57. #endif
  58. {
  59. if (object.Equals(storage, value)) return false;
  60. storage = value;
  61. this.OnPropertyChanged(propertyName);
  62. return true;
  63. }
  64. }
  65. }