Skip to content

Latest commit

 

History

History
259 lines (211 loc) · 6.51 KB

mvvm-common-snippets.md

File metadata and controls

259 lines (211 loc) · 6.51 KB

MVVM Common Snippets

This contains snippets for base classes/common blocks using while developing .NET application using MVVM design pattern.

  1. Base classes
  2. Common blocks

Base classes

Without framework

  • ModelBase
namespace Naxam.Practices 
{
    using System.ComponentModel;
    using System.Runtime.CompilerServices;

    // Use PropertyChanged.Fody to simply property declaration
    [AddINotifyPropertyChangedInterface]
    public abstract class ModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged([CallerMemberName]string propertyName = null) {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected void SetProperty<T>(ref T property, T newValue, [CallerMemberName]string propertyName = null) {
            if (Equals(property, newValue)) return;

            property = newValue;

            RaisePropertyChanged(propertyName);
        }
    }
}
  • ViewModelBase
namespace Naxam.Practices 
{
    using System.ComponentModel;
    using System.Runtime.CompilerServices;

    // Use PropertyChanged.Fody to simply property declaration
    [AddINotifyPropertyChangedInterface]
    public abstract class ViewModelModelBase : ModelBase
    {
    }
}

With MvvmCross

  • ModelBase
namespace Naxam.Practices 
{
    using MvvmCross.Core.ViewModels;

    // Use PropertyChanged.Fody to simply property declaration
    [AddINotifyPropertyChangedInterface]
    public abstract class ModelBase : MvxNotifyPropertyChanged
    {
    }
}
  • ViewModelBase
namespace Naxam.Practices 
{
    using MvvmCross.Core.ViewModels;

    // Use PropertyChanged.Fody to simply property declaration
    [AddINotifyPropertyChangedInterface]
    public abstract class ViewModelBase : MvxViewModel
    {
    }
}

With FreshMvvm

  • ModelBase
namespace Naxam.Practices 
{
    // Use PropertyChanged.Fody to simply property declaration
    [AddINotifyPropertyChangedInterface]
    public abstract class ModelBase
    {
    }
}
  • ViewModelBase
namespace Naxam.Practices 
{
    using FreshMvvm;

    public abstract class ViewModelBase : FreshBasePageModel
    {
    }
}

With Prism

  • ModelBase
namespace Naxam.Practices 
{
    using Prism.Mvvm;

    public abstract class ModelBase : BindableBase
    {
    }
}
  • ViewModelBase
namespace Naxam.Practices 
{
    using Prism;
    using Prism.Mvvm;
    using Prism.Navigation;

    public abstract class ViewModelBase : ModelBase, INavigationAware
    {
        public virtual void OnNavigatedFrom(NavigationParameters parameters) {
        }

        public virtual void OnNavigatedTo(NavigationParameters parameters) {
        }

        public virtual void OnNavigatingTo(NavigationParameters parameters) {
        }
    }

    public class EmbedableViewModelBase : ViewModelBase, IActiveAware, IDestructible
    {
        public event EventHandler IsActiveChanged;

        bool _IsActive;
        public bool IsActive {
            get => _IsActive;
            set {
                SetProperty(ref _IsActive, value, HandleIsActiveChanged);
            }
        }

        protected bool HasInitialized { get; set; }

        public virtual void OnActiveChanged(IsActiveChangedEventArgs args) {
        }
    
        public virtual void Destroy() {
        }

        void HandleIsActiveChanged() {
            OnActiveChanged(new IsActiveChangedEventArgs(IsActive));
            IsActiveChanged?.Invoke(this, new IsActiveChangedEventArgs(IsActive));
        }
    }

    public class IsActiveChangedEventArgs : EventArgs
    {
        public bool IsActive { get; private set; }

        public IsActiveChangedEventArgs(bool isActive) {
            IsActive = isActive;
        }
    }
}

Common blocks

  • prop, propg - must be modified to generate code in single line of code

  • cpropf - Create new command property with both CanExecute and Execute methods

ICommand _$name$Command;
public ICommand $name$Command => _$name$Command 
    ??= new Command<$type$>(Execute$name$Command, CanExecute$name$Command); 
bool CanExecute$name$Command($type$ parameter) { return true; }
void Execute$name$Command($type$ parameter) {}
  • cprop - Create new command property with only Execute method
ICommand _$name$Command;
public ICommand $name$Command => _$name$Command 
    ??= new Command<$type$>(Execute$name$Command); 
void Execute$name$Command($type$ parameter) {}
  • oprop - Create new observable property
$type$ _$name$;
public $type$ $name$ {
    get => _$name$;
    set => SetProperty(ref _$name$, value);
}

NOTE Don't define oprop if we are using PropertyChanged.Fody package

  • bprop - Creaet new bindable property
public static readonly BindableProperty $Name$Property = BindableProperty.Create(
    nameof($Name$),
    typeof($Type$),
    typeof($Class$),
    default($Type$),
    BindingMode.OneWay);
public $Type$ $Name$ {
    get => ($Type$)GetValue($Name$Property);
    set => SetValue($Name$Property, value);
}
  • aprop - Creaet new attached property
public static readonly BindableProperty $Name$Property = BindableProperty.CreateAttached(
    "$Name$",
    typeof($Type$),
    typeof($Class$),
    default($Type$),
    BindingMode.OneWay);
public static $Type$ Get$Name$(BindableObject obj) => ($Type$)obj.GetValue($Name$Property);
public static void Set$Name$(BindableObject obj, $Type$ value)  => obj.SetValue($Name$Property, value);

Visual Studio Configuration

Visual Studio for Mac

  1. Open VSfMac Preference pane

  1. Add or Edit

Text Editor > Code Snippets > Add (or Edit)

  1. Snippet Editor

Visual Studio for Windows

It's a bit hard to add snippet to Visual Studion on Windows because the snippet is written in XML without a UI tool.

Snippet Designer is a good extension to help us here.

Snippet Designer