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

[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.