Pages - Menu

Thursday, April 6, 2017

INotifyPropertyChange without boilerplate code in Xamarin.Forms

Implementing INotifyPropertyChange is pretty straightforward. Usually, you create a base ViewModel class which implements it and which usually contains RaisePropertyChanged method:

public abstract class BaseViewModel : INotifyPropertyChanged
{
    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(
        [CallerMemberNamestring propertyName = "")
    {
        PropertyChanged?.Invoke(thisnew PropertyChangedEventArgs(propertyName));
    }
    #endregion
}

Now you can extend the BaseViewModel and use it this way:
public class UserViewModel : BaseViewModel
{
    private string login;
    public string Login
    {
        get
        {
            return login;
        }
        set
        {
            if (login == value)
                return;

            login = value;
            RaisePropertyChanged();
        }
    }

    private string password;
    public string Password
    {
        get
        {
            return password;
        }
        set
        {
            if (password == value)
                return;

            password = value;
            RaisePropertyChanged();
        }
    }
}

For very small applications it can be a good enough approach, however, in bigger applications it turns into a lot of boring boilerplate code. Here is where NotifyPropertyChanged.Fody comes into play! With this nice package our code will turn into:

[ImplementPropertyChanged]
public abstract class BaseViewModel {}

public class UserViewModel : BaseViewModel
{
    public string Login { getset; }
    public string Password { getset; }
}

Easy as it is! I highly recommend to get familiar with the documentation as it contains a lot of useful information about more advanced flows. For example, if you need to RaisePropertyChange for dependent properties or to skip equality comparison.

Sunday, April 2, 2017

Though about optimisation of work with XAML in Xamarin.Forms

What is great about Xamarin.Forms? XAML of course! Especially if you are familiar with it from WPF / Silverlight times. However, the experience with XAML in Xamarin.Forms is totally different. Unfortunately, you will not have such a great intellisense, by default you will have to discover typos in XAML at runtime, no visual editor (yet) and without preview. I have been using VS 2017 on Windows and VS For Mac on macOS, in both cases problems listed above exists.

There are a lot of threads on stackoverflow about these problems and I am repeating myself, again and again, so I decided to write a post about it. I don't have a magic solution, just a few tricks and a though.

If you are already familiar with XAML and Xamarin.Forms and you don't care that much about intellisence you can turn on XAML compilation to catch the typos at compile time.
You can enable it at the assembly level, by adding the next line of code to your AssemblyInfo.cs:
[assembly: XamlCompilation(XamlCompilationOptions.Compile);
Or turn XAML compilation at the class level, just the next line above class declaration:
[XamlCompilation (XamlCompilationOptions.Compile)];
More detailed information can be found here.

FYI: If you set the BindingContext inside XAML you may meet this bug.

Remember that all you do in XAML is compiled to code in the end. That means that if the IDE is not working that great with XAML for Xamarin.Forms at this time, you can write everything in plain C#!
Sounds weird, however, all the problems listed above will be solved - except preview. But I find it attractive enough to try. Defining your UI layout in code behind will not violate any of MVVM principles as far as it's not going to include any business logic.

If you know any other tricks please share.
Have a nice week.