Daniel Hindrikes
Developer and architect with focus on mobile- and cloud solutions!
Developer and architect with focus on mobile- and cloud solutions!
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.
All snippets are available in the repository for TinyMvvm.
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> 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) => {
}
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);
}
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();
}
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();
}
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();
}
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;
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.
If you want to read more about TinyMvvm, I recommend the Get Started Tutorial, and also these blog posts:
The full source code and documentation for TinyMvvm can be found on GitHub. Feedback and contributions are very welcome.