BaseModel.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.ComponentModel;
  3. using System.Runtime.CompilerServices;
  4. using System.Collections.Generic;
  5. namespace XamarinInsights
  6. {
  7. public class BaseModel: INotifyPropertyChanged
  8. {
  9. public event PropertyChangedEventHandler PropertyChanged;
  10. public BaseModel ()
  11. {
  12. }
  13. internal bool ProcPropertyChanged<T> (ref T currentValue, T newValue, [CallerMemberName] string propertyName = "")
  14. {
  15. return PropertyChanged.SetProperty (this, ref currentValue, newValue, propertyName);
  16. }
  17. internal void ProcPropertyChanged (string propertyName)
  18. {
  19. if (PropertyChanged != null) {
  20. PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
  21. }
  22. }
  23. }
  24. public static class BaseNotify
  25. {
  26. public static bool SetProperty<T> (this PropertyChangedEventHandler handler, object sender, ref T currentValue, T newValue, [CallerMemberName] string propertyName = "")
  27. {
  28. if (EqualityComparer<T>.Default.Equals (currentValue, newValue))
  29. return false;
  30. currentValue = newValue;
  31. if (handler == null)
  32. return true;
  33. handler.Invoke (sender, new PropertyChangedEventArgs (propertyName));
  34. return true;
  35. }
  36. }
  37. }