← Back to list

Boost Your Xamarin App Development With Meta Programming

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

Ahmed Fouad · 2019-08-24 22:48 · 53 claps · 1.7 min read
#xamarin #xamarin-forms #postsharp #metaprogramming #codeproject
Open on Medium ↗
Wiki topics: 💻 · Programming

Boost Your Xamarin App Development With Meta Programming

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

Meta Programming in .net book

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

[embed]Xamarin.MetaProgramming 1.0.0 Package Descriptionwww.nuget.org

[embed]PostSharp 6.2.11 The #1 pattern-aware extension to C# and VB.www.nuget.org

3- Create the ViewModel

[embed]

4- Create the ViewModel Configuration Class

[embed]

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

[ViewModel(typeof(MainPageViewModelConfiguration))]

so the MainviewModel Now look like this

[ViewModel(typeof(MainPageViewModelConfiguration))]                                    public class MainPageViewModel
{
   [DefaultValue("xx")]                                        
   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

<StackLayout>
  <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 feedspot.com 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.


메타데이터
post_id
a27bbd0b87a9
slug
boost-your-xamarin-app-development-with-meta-programming-a27bbd0b87a9
url
https://medium.com/@csharpwriter/boost-your-xamarin-app-development-with-meta-programming-a27bbd0b87a9
canonical_url
https://medium.com/@csharpwriter/boost-your-xamarin-app-development-with-meta-programming-a27bbd0b87a9
author_url
https://medium.com/@csharpwriter
status
ok
fetched_at
2026-08-06 06:41:47