Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

FAQ | Preferences

James Montemagno edited this page Oct 2, 2018 · 4 revisions

Here are some common questions about the Preferences API:

Is it possible to leverage data binding with preference?

When you developing with Xamarin.Forms you may want to use data binding with a preference. For example you may have a switch that is bound to a boolean:

<Switch IsToggled="{Binding IsActive, Mode=TwoWay}"/>

In your ViewModel you could write a property that uses the Preferences API to get, set, and raise a property notification:

const string ActiveKey = "is_active_key";

public bool IsActive
{
    get => Preferences.Get(ActiveKey , false);
    set
    {
        if (IsActive == value)
            return;

         Preferences.Set(ActiveKey , value);
         OnPropertyChanged(nameof(IsActive));
    }
}
Clone this wiki locally