Boost Your Xamarin App Development With Meta Programming

Ahmed Fouad

“ Metaprograms are programs that manipulate themselves or other programs as data.”
Anders Hejlsberg

Meta Programming in .net book

The main problem of MVVM pattern used in xamarin is the boilerplate code , starting from the implementation of INotifyPropertyChanged to the Command ChangeCanExecute method calling.

While a lotof Mvvm Frameworks like Prism , MvvmLight , …attempted to tackle this issue , none of them succeed.

In this article i will show you how we will use the metaprogramming to get rid of the boilerplate’s and to make our code fully declarative.

Hello World using xamarin and Meta Programming

1- Create a new xamarin application “Hello Meta Programming”

2- Install the meta programming nuget package and postsharp package in the .net standard project

3- Create the ViewModel

4- Create the ViewModel Configuration Class

this class must be Serializable as it will be used to tell the compiler how to wave your view model class.

this.NotifyPropertyChange(model =>model.Name );

tell the compiler to add PropertyChanged event firing in the property “Name” in the view model.

this.NotifyPropertyChange(model=>model.WelcomeMsg).DependOn(model =>model.Name );

Same as the previous one plus that when the Property “Name” will get changed the PropertyChanged event will be raised for the welcomeMsg too.

this.NotifyPropertyChange(model => model.Command).DependOn(model => model.Name);

As the Command Property in the viewmodel is of type Command so it will be the event CanExecuteChanged how will get fired when the property name change.

5- Add this attribute on the view model


so the MainviewModel Now look like this

[ViewModel(typeof(MainPageViewModelConfiguration))]                                    public class MainPageViewModel
public string Name { get; set; }
public string WelcomeMsg => $"Hello {Name}"; public ICommand Command { get; }

public MainPageViewModel()
Command = new Command(() => { }, () => Name.Length > 3);
} }

6- Finally the view

<Entry Text="{Binding Name , Mode=TwoWay}" /> <Label Text="{Binding WelcomeMsg}" /> <Button Text="Submit" Command="{Binding Command}" /> </StackLayout>

As you may noticed i have used the DefaultValue Attribute in the view model , it is one of the side advantages of xamarin.MetaProgramming we have implemented the required logic to allow the DefaultValue Attribute to work.

Thanks to everyone who is sharing my blog posts last week we get featured in as one of the best 60 xamarin blogs and Postsharp offered me a free license, I hope to hear more from you in the comments.

Ahmed Fouad

Written by

Hello, I’m Ahmed. I’m a software engineer living in Vienna, Austria. I am a fan of technology, web development, and programming. I’m also interested in xamarin

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade