Skip to content

Instantly share code, notes, and snippets.

@ginomessmer
Last active December 21, 2017 13:21
Show Gist options
  • Save ginomessmer/a3dca690d30919502521 to your computer and use it in GitHub Desktop.
Save ginomessmer/a3dca690d30919502521 to your computer and use it in GitHub Desktop.
Base class for ViewModels, implements OnPropertyChanged. Nothing further to do.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>mvvm-viewmodelbase</Title>
<Author>
</Author>
<Description>
</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>mvvmviewmodelbase</Shortcut>
</Header>
<Snippet>
<Code Language="csharp" Delimiter="$"><![CDATA[public partial class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
{
if (Equals(storage, value))
{
return;
}
storage = value;
OnPropertyChanged(propertyName);
}
protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
using System.ComponentModel;
using System.Runtime.CompilerServices;
// TODO: Namespace
/// <summary>
/// Base class for ViewModels, implements OnPropertyChanged. Nothing further to do.
/// </summary>
public partial class ViewModelBase : INotifyPropertyChanged
{
public ViewModelBase()
{
}
#region Property Changed
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment