<commit_before>---
layout: post
title:  "Indexed ViewModelLocator With MvvmLight And Xamarin Forms"
date:   2016-04-03
---

<p class="intro">
<span class="dropcap">W</span>orking with MVVMLight toolkit and Xamarin Forms removes a lot of boiler plate coding. When it comes to the basic of resolving viewmodels this clean code gets a bit blurry, so let's see how we can improve this.  
</p>

### The default Viewmodel resolving
Building a solution with MVVMLight, the most common solution to resolve viewmodels is a mix of SimpleIOC and a ViewModelLocator. The ViewModelLocator provides basic binding for each ViewModel it exposes and Views consume the ViewModels by databinding.
{% highlight xml %}
<ContentPage
	xmlns="http://xamarin.com/schemas/2014/forms"
	xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
	x:Class="BasicMvvmLight.Views.CarListView"
	BindingContext="{Binding CarVM, Source={StaticResource ViewModelLocator}}">
	<ContentPage.Content>
		<Label
			Text="{Binding Brand}">
		</Label>
	</ContentPage.Content>
</ContentPage>
{% endhighlight %}

{% highlight csharp %}
public class ViewModelLocator
{
    public ViewModelLocator ()
    {<commit_msg>title fix<commit_after>---
layout: post
title:  "Indexed ViewModelLocator With MVVMLight And Xamarin Forms"
date:   2016-04-03
---

<p class="intro">
<span class="dropcap">W</span>orking with MVVMLight toolkit and Xamarin Forms removes a lot of boiler plate coding. When it comes to the basic of resolving viewmodels this clean code gets a bit blurry, so let's see how we can improve this.  
</p>

### The default Viewmodel resolving
Building a solution with MVVMLight, the most common solution to resolve viewmodels is a mix of SimpleIOC and a ViewModelLocator. The ViewModelLocator provides basic binding for each ViewModel it exposes and Views consume the ViewModels by databinding.
{% highlight xml %}
<ContentPage
	xmlns="http://xamarin.com/schemas/2014/forms"
	xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
	x:Class="BasicMvvmLight.Views.CarListView"
	BindingContext="{Binding CarVM, Source={StaticResource ViewModelLocator}}">
	<ContentPage.Content>
		<Label
			Text="{Binding Brand}">
		</Label>
	</ContentPage.Content>
</ContentPage>
{% endhighlight %}

{% highlight csharp %}
public class ViewModelLocator
{
    public ViewModelLocator ()
    {
{'fraction_non_alphanumeric': 0.10042918454935622, 'fraction_numerical': 0.013733905579399141, 'mean_word_length': 4.976923076923077, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6651119', 'n_tokens_mistral': 769, 'n_tokens_neox': 691, 'n_words': 243}