Xamarin code snippets

This will contain a list of the code snippets I have made myself (or stolen from others) that I use on a regular basis. Feel free to do with them whatever you like. I store them here so I can always grab them when I switch machines or reinstall VS.

Command Property

1
2
3
4
5
6
7
8
private ICommand _$backingname$;
public ICommand $propertyname$
{
get
{
return _$backingname$ = _$backingname$ ?? new Command(() => $methodtocall$());
}
}

Command Property Async

1
2
3
4
5
6
7
8
private ICommand _$backingname$;
public ICommand $propertyname$
{
get
{
return _$backingname$ = _$backingname$ ?? new Command(async () => await $methodtocall$());
}
}

Property with private backing and call to OnChanged()

Used with a base view model that implements INotifyPropertyChanged (e.g. MVVMHelpers)

1
2
3
4
5
6
7
8
9
10
11
12
13
private $type$ _$lowercasename$;
public $type$ $name$
{
get
{
return _$lowercasename$;
}
set
{
_$lowercasename$ = value;
OnChanged();
}
}

Safe area on iOS in XAML page

1
2
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" 
ios:Page.UseSafeArea="true"

Start a Grid in XAML page

1
2
3
4
5
6
7
8
9
10
11
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
$selected$$end$
</Grid>

Bind to viewmodel in XAML

1
2
3
4
5
xmlns:ViewModels="using:$projectname$.ViewModels">

<ContentPage.BindingContext>
<ViewModels:$viewmodelname$ />
</ContentPage.BindingContext>

Sorround with StackLayout

1
2
3
<StackLayout>
$selected$$end$
</StackLayout>