public void ApplyBindings(Activity activity, string viewModelPropertyName, int layoutResourceId) { if (ApplicationContextHolder.Context == null) { ApplicationContextHolder.Context = activity.ApplicationContext; } List <XElement> elements = null; if (layoutResourceId > -1) { /* Load the XML elements of the view. */ elements = GetLayoutAsXmlElements(activity, layoutResourceId); } if (elements != null && elements.Any()) { var rootView = activity.Window.DecorView.FindViewById(Android.Resource.Id.Content); /* Get all the binding inside the XML file. */ var bindingInfos = ExtractBindingsFromLayoutFile(elements, rootView); 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(); } } } InternalBindingApplicator binder = new InternalBindingApplicator(); foreach (var bindingInfo in bindingInfos) { IValueConverter valueConverter = null; string valueConverterName = bindingInfo.Converter; if (!string.IsNullOrWhiteSpace(valueConverterName)) { var markupExtensionInfo = markupExtensionUtil.CreateMarkupExtensionInfo(valueConverterName); if (markupExtensionInfo != null) { IMarkupExtension extension = binder.RetrieveExtension(markupExtensionInfo); if (iocContainer == null) { iocContainer = Dependency.Resolve <IContainer>(); } valueConverter = (IValueConverter)extension.ProvideValue(iocContainer); } else { Type valueConverterType; if (typeResolver.TryResolve(valueConverterName, out valueConverterType)) { //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 <XElement> elements = null; if (layoutResourceId > -1) { /* Load the XML elements of the view. */ elements = GetLayoutAsXmlElements(context, layoutResourceId); } if (elements != null && elements.Any()) { /* Get all the binding inside the XML file. */ var bindingInfos = ExtractBindingsFromLayoutFile(elements, view); 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 InternalBindingApplicator(); foreach (var bindingInfo in bindingInfos) { IValueConverter valueConverter = null; string valueConverterName = bindingInfo.Converter; if (!string.IsNullOrWhiteSpace(valueConverterName)) { var markupExtensionInfo = markupExtensionUtil.CreateMarkupExtensionInfo(valueConverterName); if (markupExtensionInfo != null) { IMarkupExtension extension = binder.RetrieveExtension(markupExtensionInfo); if (iocContainer == null) { iocContainer = Dependency.Resolve <IContainer>(); } valueConverter = (IValueConverter)extension.ProvideValue(iocContainer); } else { 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); } } }