Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

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

$
0
0

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?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>