Skip to content

Instantly share code, notes, and snippets.

@formix
Last active March 5, 2019 22:26
Show Gist options
  • Select an option

  • Save formix/2f5517b47c89d262f5b5b66005d9cd7a to your computer and use it in GitHub Desktop.

Select an option

Save formix/2f5517b47c89d262f5b5b66005d9cd7a to your computer and use it in GitHub Desktop.

Revisions

  1. formix revised this gist Mar 5, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ViewModelBase.cs
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    using System.Collections.Generic;
    using System.ComponentModel;

    namespace Tekmar.Gateway486.Mobile.Core.ViewModels
    namespace Template.Project.ViewModels
    {
    public class ViewModelBase : INotifyPropertyChanged
    {
  2. formix created this gist Nov 26, 2018.
    44 changes: 44 additions & 0 deletions ViewModelBase.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    namespace Tekmar.Gateway486.Mobile.Core.ViewModels
    {
    public class ViewModelBase : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler PropertyChanged;

    private readonly Dictionary<string, object> _properties;


    public ViewModelBase()
    {
    // Makes sure that the internal hash table won't get resized
    // while being filled.
    var projectedSize = GetType().GetProperties().Length / 0.7;
    var size = (int)Math.Ceiling(projectedSize);
    _properties = new Dictionary<string, object>(size);
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
    PropertyChanged?.Invoke(this,
    new PropertyChangedEventArgs(propertyName));
    }

    protected virtual T Get<T>(string propertyName)
    {
    if (!_properties.ContainsKey(propertyName))
    {
    return default(T);
    }
    return (T)_properties[propertyName];
    }

    protected virtual void Set(string propertyName, object value)
    {
    _properties[propertyName] = value;
    OnPropertyChanged(propertyName);
    }
    }
    }