Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Switch View not binding properly to Xam.Plugins.Settings object when IsToggled goes false

Using the Switch in a plain vanilla mvvm data binding with Prism and PropertyChanged.Fody all works fine and as expected.

Then I added James Montemagno's Settings Plugin and tried binding directly to the settings per the example here (so I'm not allowed to post links so just google James' Settings Plugin docs page on databinding to settings)

My xaml page has a simple binding

<Switch IsToggled="{Binding EnableSounds}" />

My page viewmodel uses the static Settings class which has a boolean EnableSounds property...

public bool EnableSounds {
    get { return Settings.EnableSounds; } 
    set
    {
        if (value == Settings.EnableSounds)
            return;

        Settings.EnableSounds = value;
        RaisePropertyChanged();
    }
}

When IsToggled changes from false to true, the setter is called as expected. When IsToggled changes from true to false, the setter is not called. I put a breakpoint in the setter and it is never hit when the switch is turned off, but is hit every time it is turned on again.

Adding in a private backing field seems to fix this, even though it is never really used, so the following works

private bool _enableSounds;
public bool EnableSounds {
    get { return Settings.EnableSounds; } 
    set
    {
        if (value == _enableSounds)
            return;

        _enableSounds = value;
        Settings.EnableSounds = value;
        RaisePropertyChanged();
    }
}

So I have a slightly messy workaround that has got me going, but I am totally stumped as to why it doesn't work, and why it only doesn't work when the switch is set to false.

Can anybody shed any light on this?

Sign In or Register to comment.