Skip to content

Instantly share code, notes, and snippets.

@devjaime
Created April 17, 2019 04:41
Show Gist options
  • Select an option

  • Save devjaime/cc1a98cf1703264347f91bcdb6514230 to your computer and use it in GitHub Desktop.

Select an option

Save devjaime/cc1a98cf1703264347f91bcdb6514230 to your computer and use it in GitHub Desktop.
ObservableObject.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Acelerometro.ViewModel
{
public class ObservableObject : INotifyPropertyChanged
{
protected virtual bool SetProperty<T>(ref T backingStore, T value, [CallerMemberName]string propertyName = "", Action onChanged = null, Func<T, T, bool> validateValue = null)
{
if (EqualityComparer<T>.Default.Equals(backingStore, value))
return false;
if (validateValue != null && !validateValue(backingStore, value))
return false;
backingStore = value;
onChanged?.Invoke();
OnPropertyChanged(propertyName);
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment