public void ApplyBindings(Activity activity, string viewModelPropertyName, int layoutResourceId) { if (ApplicationContextHolder.Context == null) { ApplicationContextHolder.Context = activity.ApplicationContext; } List <View> views = null; List <XElement> elements = null; if (layoutResourceId > -1) { /* Load the XML elements of the view. */ elements = GetLayoutAsXmlElements(activity, layoutResourceId); } /* If there is at least one 'Binding' attribute set in the XML file, * get the view as objects. */ if (elements != null && elements.Any(xe => xe.Attribute(bindingXmlNamespace) != null)) { var rootView = activity.Window.DecorView.FindViewById(Id.Content); views = GetAllChildrenInView(rootView); /* Remove the first views that are not part of the layout file. * This includes the first FrameView. */ int elementsCount = elements.Count; int difference = views.Count - elementsCount; if (difference > 0) { views = views.GetRange(difference, views.Count - difference); } } if (elements != null && elements.Any() && views != null && views.Any()) { /* Get all the binding inside the XML file. */ var bindingInfos = ExtractBindingsFromLayoutFile(elements, views); if (bindingInfos == null || !bindingInfos.Any()) { return; } /* Load all the converters if there is a binding using a converter. */ if (bindingInfos.Any(info => !string.IsNullOrWhiteSpace(info.Converter))) { if (valueConverterTypes == null) { valueConverterTypes = new List <Type>(); var converters = TypeUtility.GetTypes <IValueConverter>().ToList(); if (converters.Any()) { valueConverterTypes.AddRange(converters); } else if (Debugger.IsAttached) { Debugger.Break(); } } } var binder = new BindingApplicator(); foreach (var bindingInfo in bindingInfos) { IValueConverter valueConverter = null; string valueConverterName = bindingInfo.Converter; if (!string.IsNullOrWhiteSpace(valueConverterName)) { var valueConverterType = valueConverterTypes.FirstOrDefault(t => t.Name == valueConverterName); if (valueConverterType != null) { var valueConverterConstructor = valueConverterType.GetConstructor(Type.EmptyTypes); if (valueConverterConstructor != null) { valueConverter = valueConverterConstructor.Invoke(null) as IValueConverter; } else { throw new InvalidOperationException( $"Value converter {valueConverterName} needs " + "an empty constructor to be instanciated."); } } else { throw new InvalidOperationException( $"There is no converter named {valueConverterName}."); } } binder.ApplyBinding(bindingInfo, activity, viewModelPropertyName, valueConverter, unbindActions); } } }
public void ApplyBindings(View view, object dataContext, int layoutResourceId) { Context context = view.Context; if (ApplicationContextHolder.Context == null) { ApplicationContextHolder.Context = context.ApplicationContext; } List <View> views = null; List <XElement> elements = null; if (layoutResourceId > -1) { /* Load the XML elements of the view. */ elements = GetLayoutAsXmlElements(context, layoutResourceId); } /* If there is at least one 'Binding' attribute set in the XML file, * get the view as objects. */ if (elements != null && elements.Any(xe => xe.Attribute(bindingXmlNamespace) != null)) { views = GetAllChildrenInView(view); } if (elements != null && elements.Any() && views != null && views.Any()) { /* Get all the binding inside the XML file. */ var bindingInfos = ExtractBindingsFromLayoutFile(elements, views); if (bindingInfos == null || !bindingInfos.Any()) { return; } /* Load all the converters if there is a binding using a converter. */ if (bindingInfos.Any(bo => !string.IsNullOrWhiteSpace(bo.Converter))) { if (valueConverterTypes == null) { valueConverterTypes = new List <Type>(); var converters = TypeUtility.GetTypes <IValueConverter>(); valueConverterTypes.AddRange(converters); } } var binder = new BindingApplicator(); foreach (var bindingInfo in bindingInfos) { IValueConverter valueConverter = null; string valueConverterName = bindingInfo.Converter; if (!string.IsNullOrWhiteSpace(valueConverterName)) { var converterType = valueConverterTypes.FirstOrDefault(t => t.Name == valueConverterName); if (converterType != null) { var constructor = converterType.GetConstructor(Type.EmptyTypes); if (constructor != null) { valueConverter = constructor.Invoke(null) as IValueConverter; } else { throw new InvalidOperationException( $"Value converter {valueConverterName} needs " + "an empty constructor to be instanciated."); } } else { throw new InvalidOperationException( $"There is no converter named {valueConverterName}."); } } binder.ApplyBinding(bindingInfo, dataContext, valueConverter, unbindActions); } } }