Snippets for TinyMvvm

This blog post is a part of the Xamarin Month, a great initiative by Luis Matos. The theme for this Xamarin Month is code snippets. Here is my contribution to the Xamarin Month.

TinyMvvm is a library that I created to make me more productive when I developing apps with Xamarin.Forms.

Snippets

All snippets are available in the repository for TinyMvvm.

TinyCommand (tmcmd)

TinyMvvm has its own implementation of ICommand. You can use Xamarin.Forms Command, but if you use the TinyCommand you don't have to add references to Xamarin.Forms in your ViewModels.

Shortcut: tmcmd

private ICommand? command;
public ICommand Command => command ??= new TinyCommand(async() => {

}

TinyCommand<T> (tmcmdt)

TinyCommand<T> is a generic TinyCommand that is able to handle a parameter.

Shortcut: tmcmdt

private ICommand? command;
public ICommand Command => command ??= new TinyCommand<object>(async(parameter) => {

}

TinyMvvm property (tmprop)

I often use PropertyChanged.Fody so I don't need this snippet, but if you don't use it, this snippet can be very useful.

Shortcut: tmprop

private string propertyName;
public string PropertyName
{
    get => propertyName;
    set => Set(ref propertyName, value);
}

Override Initialize (tminit)

In the ViewModel you can override Initialize if you have ViewModelBase as your base class. It will run when the ViewModel is created.

Shortcut: tminit

public async override Task Initialize()
{
    await base.Initialize();
}

Override OnAppearing (tmapp)

In the ViewModel you can override OnAppearing if you have ViewModelBase as your base class. It will be triggered by when the View is appearing.

Shortcut: tmapp

public async override Task OnAppearing()
{
    await base.OnAppearing();
}

Override OnDisappearing (tmdis)

In the ViewModel you can override OnDisappearing if you have ViewModelBase as your base class. It will be triggered by when the View is disappearing.

Shortcut: tmdis

public async override Task OnDisappearing()
{
    await base.OnDisappearing();
}

IsBusy (IsBusy)

In the ViewModelBase there is a property with the name IsBusy. I use to set it to true when I loading data so I can bind an ActivityIndicator to it. There is also a property with the name IsNotBusy that always will have the opposite value of IsBusy. This snippet can be used to surround your code with IsBusy.

Shortcut: IsBusy

IsBusy = true;
//Your code
IsBusu = false;

Import snippets

For Visual Studio on Windows, snippets are imported with the Code Snippets Manager that you will find in the Tools menu. Read more here, https://docs.microsoft.com/en-us/visualstudio/ide/walkthrough-creating-a-code-snippet?view=vs-2019#import-a-code-snippet

For Visual Studio for Mac, add them in the ~/Library/VisualStudio/8.0/Snippets folder.

Read more

If you want to read more about TinyMvvm, I recommend the Get Started Tutorial, and also these blog posts:

Contribute

The full source code and documentation for TinyMvvm can be found on GitHub. Feedback and contributions are very welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.