private DataBinding AddBinding(BindingParameters bp) { if (bp.View != null && FindBindingForView(bp.View) != null) { throw new ArgumentException("Cannot add binding because a binding already exists for the view " + bp.View.ToString()); } if (dataBindings.ContainsKey(IdName(bp.ViewModelPropertyName))) { throw new ArgumentException("Cannot add binding because a binding already exists for the view with Id " + IdName(bp.ViewModelPropertyName)); } var view = bp.View; if (view == null) { return(null); } var viewMemberName = bp.ViewMemberName; if ((bp.Mode == BindingMode.OneWay || bp.Mode == BindingMode.TwoWay) && bp.UpdateView == null && viewMemberName == null) { ViewDefaultPropertyOrFieldName.TryGetValue(view.GetType().FullName, out viewMemberName); } var propertyName = bp.ViewModelPropertyName; var mode = bp.Mode; var listPropertyName = bp.ListPropertyName; var itemTemplateName = bp.ListItemTemplateName; var idName = IdName(propertyName); var viewModelPropertyInfo = (string.IsNullOrEmpty(propertyName) || propertyName == ".") ? null : viewModel.GetType().GetProperty(propertyName); var binding = new DataBinding { ViewProperty = new PropertyReference(view, viewMemberName, viewModelPropertyInfo != null ? viewModelPropertyInfo.PropertyType : null), UpdateViewAction = bp.UpdateView, UpdateViewModelAction = bp.UpdateViewModel, Mode = mode, ViewModelPropertyInfo = viewModelPropertyInfo }; if (listPropertyName == null) { listPropertyName = propertyName + "List"; } var pi = viewModel.GetType().GetProperty(listPropertyName); if (pi == null && binding.ViewModelPropertyInfo != null && binding.ViewModelPropertyInfo.PropertyType.GetInterface("IList") != null) { listPropertyName = propertyName; pi = binding.ViewModelPropertyInfo; binding.ViewModelPropertyInfo = null; } binding.ViewModelListPropertyInfo = pi; if (binding.ViewProperty.ContainingObject is UITableView) { var tableView = (UITableView)binding.ViewProperty.ContainingObject; if (tableView.Source != null) { binding.TableViewSource = tableView.Source as DataBindableUITableViewSource; } else { if (itemTemplateName == null) { itemTemplateName = listPropertyName + "Item"; } string listItemSelectedPropertyName = (mode == BindingMode.Command || mode == BindingMode.TwoWay) ? bp.ViewModelPropertyName : null; tableView.Source = binding.TableViewSource = new DataBindableUITableViewSource( tableView, itemTemplateName, viewModel, bp.ListCanEditItem, bp.ListCanMoveItem, listItemSelectedPropertyName, bp.ListRemoveItemCommandName, bp.ListAddItemCommandName, rootViewExtensionPoints ); } } switch (binding.Mode) { case BindingMode.TwoWay: AddTwoWayHandler(binding); break; case BindingMode.Command: AddCommandHandler(binding); break; } dataBindings.Add(idName, binding); return(binding); }
private DataBinding AddBinding(BindingParameters bp) { object view = bp.View; View androidView = view as View; string propertyName = bp.ViewModelPropertyName; if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentNullException("ViewModelPropertyName"); } string idName = IdName(propertyName); int? resourceId; if (androidView != null) { string viewIdName = AndroidHelpers.FindResourceName(androidView.Id); if (viewIdName != idName) { throw new ArgumentException(string.Format("View Id '{0}' does not match expected value '{1}'; adjust either the view Id or the ViewModelPropertyName binding parameter.", viewIdName, idName)); } resourceId = androidView.Id; } else { resourceId = AndroidHelpers.FindResourceId(idName); } if (view == null && resourceId.HasValue) { view = androidView = rootView.FindViewById(resourceId.Value); } if (view == null) { return(null); } if (androidView != null && androidView.Tag != null) { bp = ParseBindingParameters(androidView.Tag.ToString(), bp); } string viewMemberName = bp.ViewMemberName; if ((bp.Mode == BindingMode.OneWay || bp.Mode == BindingMode.TwoWay) && bp.UpdateView == null && viewMemberName == null) { ViewDefaultPropertyOrFieldName.TryGetValue(view.GetType().FullName, out viewMemberName); } var viewModelPropertyInfo = (string.IsNullOrEmpty(propertyName) || propertyName == ".") ? null : viewModel.GetType().GetProperty(propertyName); AdapterView commandParameterSelectedItemAdapterView = bp.CommandParameterSelectedItemAdapterView; if (commandParameterSelectedItemAdapterView == null && bp.ListId.HasValue) { commandParameterSelectedItemAdapterView = rootView.FindViewById <AdapterView>(bp.ListId.Value); } var binding = new DataBinding { ViewProperty = new PropertyReference(view, viewMemberName, viewModelPropertyInfo != null ? viewModelPropertyInfo.PropertyType : null), ResourceId = resourceId, UpdateViewAction = bp.UpdateView, UpdateViewModelAction = bp.UpdateViewModel, Mode = bp.Mode, ViewModelPropertyInfo = viewModelPropertyInfo, CommandParameterListId = bp.ListId, CommandParameterListView = commandParameterSelectedItemAdapterView }; string listPropertyName = bp.ListPropertyName; if (listPropertyName == null) { listPropertyName = propertyName + "List"; } var pi = viewModel.GetType().GetProperty(listPropertyName); if (pi == null && binding.ViewModelPropertyInfo.PropertyType.GetInterface("IList") != null) { listPropertyName = propertyName; pi = binding.ViewModelPropertyInfo; binding.ViewModelPropertyInfo = null; } binding.ViewModelListPropertyInfo = pi; if (view is AdapterView) { pi = view.GetType().GetProperty("Adapter", BindingFlags.Public | BindingFlags.Instance); if (pi != null) { var adapter = pi.GetValue(view); if (adapter == null) { string itemTemplateName = bp.ListItemTemplateName ?? listPropertyName + "Item"; string itemValueId = (bp.ListItemIsValue && bp.ListItemValueId == null) ? itemTemplateName : bp.ListItemValueId; int? itemTemplateResourceId = AndroidHelpers.FindResourceId(itemTemplateName, AndroidHelpers.ResourceCategory.Layout); int? itemValueResourceId = AndroidHelpers.FindResourceId(itemValueId); if (itemTemplateResourceId.HasValue) { adapter = new DataBindableListAdapter <object>(layoutInflater, itemTemplateResourceId.Value, itemTemplateName + "_", itemValueResourceId, rootViewExtensionPoints); pi.SetValue(view, adapter); } } binding.ListAdapter = adapter as IDataBindableListAdapter; } } switch (binding.Mode) { case BindingMode.TwoWay: AddTwoWayHandler(binding); break; case BindingMode.Command: AddCommandHandler(binding); break; } dataBindings.Add(idName, binding); return(binding); }