Hi All,
I am using the excellent Xamarin Plugin from James Montemagno called CrossSettings in my Xamarin Forms application and I'd love to be able to do something like the following so I could implement two-way data binding in a Xamarin Forms application.
Helpers\Settings.cs
namespace MakeTracks.Core.Helpers
{
/// <summary>
/// This is the Settings static class that can be used in your Core solution or in any
/// of your client applications. All settings are laid out the same exact way with getters
/// and setters.
/// </summary>
public static class Settings : INotifyPropertyChanged
{
#region INotifyPropertyChanged implementation`
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Protected method for firing PropertyChanged
/// </summary>
/// <param name="propertyName">The name of the property that changed</param>
protected virtual void OnPropertyChanged (string propertyName)
{
var method = PropertyChanged;
if (method != null)
method (this, new PropertyChangedEventArgs (propertyName));
}
#endregion
And then in the settings property setter I would add the following:
#region Setting Constants
// Group Notifications setting key
private const string GroupNotificationsKey = "group_notfications";
private static readonly bool GroupNotificationsDefault = true;
#endregion
public static bool GroupNotifications
{
get
{
return AppSettings.GetValueOrDefault (GroupNotificationsKey, GroupNotificationsDefault);
}
set
{
//if value has changed then save it!
AppSettings.AddOrUpdateValue (GroupNotificationsKey, value);
OnPropertyChanged (GroupNotificationsKey);
}
}
Unfortunately the Settings.cs class is static and it wont allow it to implement interfaces like this.
I have a Xamarin Forms application with a settings page that is using a tableview and I am trying to bind the controls i.e. SwitchCell to the AppSettings.Properties for each setting so the form updates based on the value of the setting.
Has anyone successfully used this in a Xamarin.Forms application settings page like this?
Is there another way to achieve this via code i.e. setting binding context, etc.?
I have tried that without much success. Here is what I tried in my Applications Settings page:
private bool _groupNotificationsSetting = Settings.GroupNotifications;
public bool MyGroupNotifications {
get {
return _groupNotificationsSetting;
}
set {
_groupNotificationsSetting = value;
}
}
this.BindingContext = Settings.GroupNotifications;
SwitchCell scGroupNotifications = new SwitchCell ();
scGroupNotifications.Text = Data.NotificationsType.Group.ToStringValue ();
// scGroupNotifications.IsEnabled = groupNotificationsSetting;
scGroupNotifications.SetBinding (SwitchCell.IsEnabledProperty, new Binding("MyGroupNotifications"));
I get the following in the Xamarin console:
2014-12-07 16:54:42.744 MakeTracksiOS[88736:8129280] Binding: 'MyGroupNotifications' property not found on 'True', target property: 'Xamarin.Forms.SwitchCell.IsEnabled'
Binding: 'MyGroupNotifications' property not found on 'True', target property: 'Xamarin.Forms.SwitchCell.IsEnabled'
Any assistance would be greatly appreciated.
Cheers,
Grant