protected virtual void InitializePreferences(PreferenceScreen preferenceScreen, int preferencesResId) { preferenceScreen.SetBindingMemberValue(AttachedMembers.Object.Parent, Target); SetPreferenceParent(preferenceScreen); using (XmlReader reader = preferenceScreen.Context.Resources.GetXml(preferencesResId)) { var document = new XmlDocument(); document.Load(reader); var xDocument = XDocument.Parse(document.InnerXml); foreach (var descendant in xDocument.Descendants()) { var bindAttr = descendant .Attributes() .FirstOrDefault(xAttribute => xAttribute.Name.LocalName.Equals("bind", StringComparison.OrdinalIgnoreCase)); if (bindAttr == null) { continue; } var attribute = descendant.Attribute(XName.Get("key", "http://schemas.android.com/apk/res/android")); if (attribute == null) { Tracer.Error("Preference {0} must have a key to use it with bindings", descendant); continue; } var preference = preferenceScreen.FindPreference(attribute.Value); BindingServiceProvider.BindingProvider.CreateBindingsFromString(preference, bindAttr.Value); } } SetPreferenceListener(); }
public static void InitializePreferences(PreferenceScreen preferenceScreen, int preferencesResId, object parent) { preferenceScreen.SetBindingMemberValue(AttachedMembers.Object.Parent, parent); SetPreferenceParent(preferenceScreen); List <KeyValuePair <string, string> > bindings; if (!PreferenceBindMapping.TryGetValue(preferencesResId, out bindings)) { bindings = new List <KeyValuePair <string, string> >(); using (var reader = preferenceScreen.Context.Resources.GetXml(preferencesResId)) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: string bind = null; for (int attInd = 0; attInd < reader.AttributeCount; attInd++) { reader.MoveToAttribute(attInd); if (reader.Name == "bind") { bind = reader.Value; break; } } reader.MoveToElement(); var key = reader.GetAttribute("key", "http://schemas.android.com/apk/res/android"); if (string.IsNullOrEmpty(bind)) { break; } if (string.IsNullOrEmpty(key)) { Tracer.Error("Preference {0} must have a key to use it with bindings", reader.Name); break; } bindings.Add(new KeyValuePair <string, string>(key, bind)); break; } } } PreferenceBindMapping[preferencesResId] = bindings; } foreach (var map in bindings) { var preference = preferenceScreen.FindPreference(map.Key); BindingServiceProvider.BindingProvider.CreateBindingsFromString(preference, map.Value); } }