示例#1
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            Title = "_VIEWNAME_";
            View.Frame = UIScreen.MainScreen.Bounds;
            View.BackgroundColor = UIColor.White;

            #region Bind to example property and command
            var button = UIButton.FromType(UIButtonType.System);
            float buttonWidth = 200, buttonHeight = 30;
            button.Frame = new RectangleF(View.Frame.Width / 2 - buttonWidth / 2, View.Frame.Height / 2 - buttonHeight / 2, buttonWidth, buttonHeight);
            button.SetTitle("Click me", UIControlState.Normal);
            View.AddSubview(button);

            var label = new UILabel(new RectangleF(button.Frame.Left, button.Frame.Bottom + 2, button.Frame.Width, button.Frame.Height));
            label.TextAlignment = UITextAlignment.Center;
            View.AddSubview(label);

            var bindingsParameters = new BindingParameters[] {
                new BindingParameters { View = button, PropertyName = _VIEWNAME_ViewModel.COMMANDNAME_IncreaseCountCommand, Mode = BindingMode.Command },
                new BindingParameters { View = label, PropertyName = _VIEWNAME_ViewModel.PROPERTYNAME_Count }
            };
            #endregion Bind to example property and command

            InitializeBindings(View, ViewModel, bindingsParameters);
        }
示例#2
0
 public void AddBindings(BindingParameters[] bindingsParameters)
 {
     if (bindingsParameters != null)
     {
         foreach (var bp in bindingsParameters)
         {
             if (bp.View != null && FindBindingForView(bp.View) != null) throw new ArgumentException("Cannot add binding because a binding already exists for the view with Id " + bp.View.Id.ToString());
             if (dataBindings.ContainsKey(IdName(bp.PropertyName))) throw new ArgumentException("Cannot add binding because a binding already exists for the view with Id " + IdName(bp.PropertyName));
             AddBinding(bp.PropertyName, bp.Mode, bp.ListPropertyName, bp.View, bp.CommandParameterSelectedItemAdapterView);
         }
     }
 }
示例#3
0
        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);
        }
示例#4
0
        private static BindingParameters ParseBindingParameters(string parameters)
        {
            BindingParameters bp = null;

            if (!string.IsNullOrEmpty(parameters))
            {
                var match = Regex.Match(parameters, @"(({Binding\s)?\s*((?<assignment>[^,{}]+),?)+\s*}?)?(\s*{List\s+((?<assignment>[^,{}]+),?)+\s*})?(\s*{CommandParameter\s+((?<assignment>[^,{}]+),?)+\s*})?");
                if (match.Success)
                {
                    var gc = match.Groups["assignment"];
                    if (gc != null)
                    {
                        var cc = gc.Captures;
                        if (cc != null)
                        {
                            bp = new BindingParameters();
                            for (int i = 0; i < cc.Count; i++)
                            {
                                string[] assignmentElements = cc[i].Value.Split('=');
                                if (assignmentElements.Length == 1)
                                {
                                    string value = assignmentElements[0].Trim();
                                    if (value != "")
                                    {
                                        bp.ViewModelPropertyName = value;
                                    }
                                }
                                else if (assignmentElements.Length == 2)
                                {
                                    string name  = assignmentElements[0].Trim();
                                    string value = assignmentElements[1].Trim();
                                    if (name.StartsWith("."))
                                    {
                                        bp.ViewMemberName = name.Substring(1);
                                        if (value != "")
                                        {
                                            bp.ViewModelPropertyName = value;
                                        }
                                    }
                                    else
                                    {
                                        switch (name)
                                        {
                                        case "Mode": Enum.TryParse <BindingMode>(value, true, out bp.Mode); break;

                                        case "ItemsSource": bp.ListPropertyName = value; break;

                                        case "ItemTemplate": bp.ListItemTemplateName = value; break;

                                        case "AddCommand": bp.ListAddItemCommandName = value; break;

                                        case "RemoveCommand": bp.ListRemoveItemCommandName = value; break;

                                        case "CanEdit": bp.ListCanEditItem = value; break;

                                        case "CanMove": bp.ListCanMoveItem = value; break;

                                        default: throw new ArgumentException("Unknown tag binding parameter: " + name);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(bp);
        }
示例#5
0
        private DataBinding AddBinding(BindingParameters bp)
        {
            var view = bp.View;

            if (view == null)
            {
                return(null);
            }
            var propertyName     = bp.PropertyName;
            var mode             = bp.Mode;
            var listPropertyName = bp.ListPropertyName;
            var itemTemplateName = bp.ListItemTemplateName;
            // TODO: , AdapterView commandParameterSelectedItemAdapterView = null

            var idName = IdName(propertyName);

            var binding = new DataBinding
            {
                View = view,
                Mode = mode,
                ViewModelPropertyInfo = (string.IsNullOrEmpty(propertyName) || propertyName == ".") ? null : viewModel.GetType().GetProperty(propertyName) // TODO,
                                                                                                                                                           // CommandParameterListId = commandParameterListId,
                                                                                                                                                           // CommandParameterListView = commandParameterSelectedItemAdapterView
            };

            if (binding.View is UITableView)
            {
                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;

                var tableView = (UITableView)binding.View;
                if (tableView.Source == null)
                {
                    if (itemTemplateName == null)
                    {
                        itemTemplateName = listPropertyName + "Item";
                    }
                    string listItemSelectedPropertyName = (mode == BindingMode.Command || mode == BindingMode.TwoWay) ? bp.PropertyName : null;
                    tableView.Source = binding.TableViewSource = new DataBindableUITableViewSource(
                        tableView,
                        itemTemplateName,
                        viewModel,
                        bp.ListCanEditItem,
                        bp.ListCanMoveItem,
                        listItemSelectedPropertyName,
                        bp.ListRemoveItemCommandName,
                        bp.ListAddItemCommandName,
                        rootViewExtensionPoints
                        );
                }
            }

            /*
             * if (binding.View is AdapterView)
             * {
             *      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;
             *
             *      pi = binding.View.GetType().GetProperty("Adapter", BindingFlags.Public | BindingFlags.Instance);
             *      if (pi != null)
             *      {
             *              var adapter = pi.GetValue(binding.View);
             *              if (adapter == null)
             *              {
             *                      if (itemTemplateName == null) itemTemplateName = listPropertyName + "Item";
             *                      if (itemIsValue && itemValueId == null) itemValueId = itemTemplateName;
             *                      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(binding.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);
        }
示例#6
0
        private static BindingParameters ParseBindingParameters(string parameters)
        {
            BindingParameters bp = null;

            if (!string.IsNullOrEmpty(parameters))
            {
                var match = Regex.Match(parameters, @"(({Binding\s)?\s*((?<assignment>[^,{}]+),?)+\s*}?)?(\s*{List\s+((?<assignment>[^,{}]+),?)+\s*})?(\s*{CommandParameter\s+((?<assignment>[^,{}]+),?)+\s*})?");
                if (match.Success)
                {
                    var gc = match.Groups["assignment"];
                    if (gc != null)
                    {
                        var cc = gc.Captures;
                        if (cc != null)
                        {
                            bp = new BindingParameters();
                            for (int i = 0; i < cc.Count; i++)
                            {
                                string[] assignmentElements = cc[i].Value.Split('=');
                                if (assignmentElements.Length == 1)
                                {
                                    string value = assignmentElements[0].Trim();
                                    if (value != "")
                                    {
                                        bp.PropertyName = value;
                                    }
                                }
                                else if (assignmentElements.Length == 2)
                                {
                                    string name  = assignmentElements[0].Trim();
                                    string value = assignmentElements[1].Trim();
                                    switch (name)
                                    {
                                    case "Mode": Enum.TryParse <BindingMode>(value, true, out bp.Mode); break;

                                    case "ItemsSource": bp.ListPropertyName = value; break;

                                    case "ItemTemplate": bp.ListItemTemplateName = value; break;

                                    case "AddCommand": bp.ListAddItemCommandName = value; break;

                                    case "RemoveCommand": bp.ListRemoveItemCommandName = value; break;

                                    case "CanEdit": bp.ListCanEditItem = value; break;

                                    case "CanMove": bp.ListCanMoveItem = value; break;

                                    // TODO: case "ItemValueId": itemValueId = value; break;
                                    // TODO: case "ListId":
                                    // commandParameterListId = AndroidHelpers.FindResourceId(value);
                                    // if (commandParameterSelectedItemAdapterView == null && commandParameterListId.HasValue) commandParameterSelectedItemAdapterView = rootView.FindViewById<AdapterView>(commandParameterListId.Value);
                                    // break;
                                    default: throw new ArgumentException("Unknown tag binding parameter: " + name);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(bp);
        }
示例#7
0
        private DataBinding AddBinding(BindingParameters bp)
        {
            var view = bp.View;
            if (view == null) return null;
            var propertyName = bp.PropertyName;
            var mode = bp.Mode;
            var listPropertyName = bp.ListPropertyName;
            var itemTemplateName = bp.ListItemTemplateName;
             // TODO: , AdapterView commandParameterSelectedItemAdapterView = null

            var idName = IdName(propertyName);

            var binding = new DataBinding
            {
                View = view,
                Mode = mode,
                ViewModelPropertyInfo = (string.IsNullOrEmpty(propertyName) || propertyName == ".") ? null : viewModel.GetType().GetProperty(propertyName) // TODO,
                // CommandParameterListId = commandParameterListId,
                // CommandParameterListView = commandParameterSelectedItemAdapterView
            };

            if (binding.View is UITableView)
            {
                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;

                var tableView = (UITableView)binding.View;
                if (tableView.Source == null)
                {
                    if (itemTemplateName == null) itemTemplateName = listPropertyName + "Item";
                    string listItemSelectedPropertyName = (mode == BindingMode.Command || mode == BindingMode.TwoWay) ? bp.PropertyName : null;
                    tableView.Source = binding.TableViewSource = new DataBindableUITableViewSource(
                        tableView,
                        itemTemplateName,
                        viewModel,
                        bp.ListCanEditItem,
                        bp.ListCanMoveItem,
                        listItemSelectedPropertyName,
                        bp.ListRemoveItemCommandName,
                        bp.ListAddItemCommandName,
                        rootViewExtensionPoints
                    );
                }
            }

            /*
            if (binding.View is AdapterView)
            {
                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;

                pi = binding.View.GetType().GetProperty("Adapter", BindingFlags.Public | BindingFlags.Instance);
                if (pi != null)
                {
                    var adapter = pi.GetValue(binding.View);
                    if (adapter == null)
                    {
                        if (itemTemplateName == null) itemTemplateName = listPropertyName + "Item";
                        if (itemIsValue && itemValueId == null) itemValueId = itemTemplateName;
                        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(binding.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;
        }
示例#8
0
        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);
        }
示例#9
0
        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;
        }
示例#10
0
 public static BindingParameters ParseBindingParameters(string tag, BindingParameters bp = null)
 {
     if (tag != null && tag.Contains("{"))
     {
         var match = Regex.Match(tag, @"({Binding\s+((?<assignment>[^,{}]+),?)+\s*})?(\s*{List\s+((?<assignment>[^,{}]+),?)+\s*})?(\s*{CommandParameter\s+((?<assignment>[^,{}]+),?)+\s*})?");
         if (match.Success)
         {
             var gc = match.Groups["assignment"];
             if (gc != null)
             {
                 var cc = gc.Captures;
                 if (cc != null)
                 {
                     if (bp == null) bp = new BindingParameters();
                     for (int i = 0; i < cc.Count; i++)
                     {
                         string[] assignmentElements = cc[i].Value.Split('=');
                         if (assignmentElements.Length == 1)
                         {
                             string value = assignmentElements[0].Trim();
                             if (value != "") bp.ViewModelPropertyName = value;
                         }
                         else if (assignmentElements.Length == 2)
                         {
                             string name = assignmentElements[0].Trim();
                             string value = assignmentElements[1].Trim();
                             if (name.StartsWith("."))
                             {
                                 bp.ViewMemberName = name.Substring(1);
                                 if (value != "") bp.ViewModelPropertyName = value;
                             }
                             else
                             {
                                 switch (name)
                                 {
                                     case "Mode"        : Enum.TryParse<BindingMode>(value, true, out bp.Mode); break;
                                     case "ItemsSource" : bp.ListPropertyName= value; break;
                                     case "ItemIsValue" : Boolean.TryParse(value, out bp.ListItemIsValue); break;
                                     case "ItemTemplate": bp.ListItemTemplateName = value; break;
                                     case "ItemValueId" : bp.ListItemValueId = value; break;
                                     case "ListId"      : bp.ListId = AndroidHelpers.FindResourceId(value); break;
                                     default: throw new ArgumentException("Unknown tag binding parameter: " + name);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return bp;
 }
示例#11
0
        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)
            {
                var typeName = view.GetType().FullName;
                if (!ViewDefaultPropertyOrFieldName.TryGetValue(typeName, out viewMemberName))
                    throw new ArgumentException(string.Format("No default property or field name exists for view type {0}. Please specify the name of a property or field in the ViewMemberName binding parameter", typeName), "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)
                {
                    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;
        }
示例#12
0
 private static BindingParameters ParseBindingParameters(string parameters)
 {
     BindingParameters bp = null;
     if (!string.IsNullOrEmpty(parameters))
     {
         var match = Regex.Match(parameters, @"(({Binding\s)?\s*((?<assignment>[^,{}]+),?)+\s*}?)?(\s*{List\s+((?<assignment>[^,{}]+),?)+\s*})?(\s*{CommandParameter\s+((?<assignment>[^,{}]+),?)+\s*})?");
         if (match.Success)
         {
             var gc = match.Groups["assignment"];
             if (gc != null)
             {
                 var cc = gc.Captures;
                 if (cc != null)
                 {
                     bp = new BindingParameters();
                     for (int i = 0; i < cc.Count; i++)
                     {
                         string[] assignmentElements = cc[i].Value.Split('=');
                         if (assignmentElements.Length == 1)
                         {
                             string value = assignmentElements[0].Trim();
                             if (value != "") bp.ViewModelPropertyName = value;
                         }
                         else if (assignmentElements.Length == 2)
                         {
                             string name = assignmentElements[0].Trim();
                             string value = assignmentElements[1].Trim();
                             if (name.StartsWith("."))
                             {
                                 bp.ViewMemberName = name.Substring(1);
                                 if (value != "") bp.ViewModelPropertyName = value;
                             }
                             else
                             {
                                 switch (name)
                                 {
                                     case "Mode": Enum.TryParse<BindingMode>(value, true, out bp.Mode); break;
                                     case "ItemsSource": bp.ListPropertyName = value; break;
                                     case "ItemTemplate": bp.ListItemTemplateName = value; break;
                                     case "AddCommand": bp.ListAddItemCommandName = value; break;
                                     case "RemoveCommand": bp.ListRemoveItemCommandName = value; break;
                                     case "CanEdit": bp.ListCanEditItem = value; break;
                                     case "CanMove": bp.ListCanMoveItem = value; break;
                                     default: throw new ArgumentException("Unknown tag binding parameter: " + name);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return bp;
 }
示例#13
0
        public void AddBindings(BindingParameters[] bindingsParameters = null, UIView rootView = null, UINavigationItem navigationItem = null)
        {
            if (bindingsParameters != null)
            {
                Console.WriteLine("Adding bindings from code ...");
                foreach (var bp in bindingsParameters) AddBinding(bp);
            }
            if (rootView != null)
            {
                List<BindingParameters> bindingParametersList;
                if (ViewDataBindings.RootViewBindingParameters.TryGetValue(rootView, out bindingParametersList))
                {
                    Console.WriteLine("Adding bindings from markup ...");
                    ViewDataBindings.RootViewBindingParameters.Remove(rootView); // Remove the static reference to the views to prevent memory leaks. Note that if we would want to recreate the bindings later, we could also store the parameters list in the bindings.
                    foreach (var bp in bindingParametersList) AddBinding(bp);
                }
            }
            if (navigationItem != null)
            {
                BindingParameters bp;
                if (ObjectBindingParameters.TryGetValue(navigationItem, out bp))
                {
                    AddBinding(bp);
                    ObjectBindingParameters.Remove(navigationItem);
                }

                var uiBarItems = new List<UIBarItem>();
                // TODO: check if we need this eg when leftItemsSupplementBackButton is true? uiBarItems.Add(navigationItem.BackBarButtonItem);
                if (navigationItem.LeftBarButtonItems != null) uiBarItems.AddRange(navigationItem.LeftBarButtonItems);
                if (navigationItem.RightBarButtonItems != null) uiBarItems.AddRange(navigationItem.RightBarButtonItems);
                // TODO: check if navigationItem.TitleView also needs Bind custom runtime attribute support?
                foreach (var uiBarItem in uiBarItems)
                {
                    if (ObjectBindingParameters.TryGetValue(uiBarItem, out bp))
                    {
                        AddBinding(bp);
                        ObjectBindingParameters.Remove(uiBarItem);
                    }
                }
            }
        }
示例#14
0
        /// <summary>
        /// Call InitializeBindings() in the ViewDidLoad method of a derived view class to create the data bindings and update the view with the current view model values.
        /// </summary>
        /// <param name="rootView">The view that should display the viewModel</param>
        /// <param name="viewModel">The view model</param>
        /// <param name="bindingsParameters">Optional binding parameters; use to override default parameter values for specific bindings, or as an alternative for specifying binding parameters in the view tag attribute in AXML. Note that any binding parameters specified in the tag attribute wil override bindingsParameters.</param>
        /// <param name="idPrefix">The name prefix used to match view Id to property name. Default value is the root view class name + "_"</param>
        protected void InitializeBindings(UIView rootView, ViewModelBase viewModel, BindingParameters[] bindingsParameters = null, string idPrefix = null)
        {
            Bindings = new ViewDataBindings(viewModel, idPrefix ?? this.GetType().Name + "_", this);
            this.viewModel = viewModel;

            EnsureHandlersAreAdded();

            Bindings.AddBindings(bindingsParameters, rootView, NavigationItem);
        }
示例#15
0
 private static BindingParameters ParseBindingParameters(string parameters)
 {
     BindingParameters bp = null;
     if (!string.IsNullOrEmpty(parameters))
     {
         var match = Regex.Match(parameters, @"(({Binding\s)?\s*((?<assignment>[^,{}]+),?)+\s*}?)?(\s*{List\s+((?<assignment>[^,{}]+),?)+\s*})?(\s*{CommandParameter\s+((?<assignment>[^,{}]+),?)+\s*})?");
         if (match.Success)
         {
             var gc = match.Groups["assignment"];
             if (gc != null)
             {
                 var cc = gc.Captures;
                 if (cc != null)
                 {
                     bp = new BindingParameters();
                     for (int i = 0; i < cc.Count; i++)
                     {
                         string[] assignmentElements = cc[i].Value.Split('=');
                         if (assignmentElements.Length == 1)
                         {
                             string value = assignmentElements[0].Trim();
                             if (value != "") bp.PropertyName = value;
                         }
                         else if (assignmentElements.Length == 2)
                         {
                             string name = assignmentElements[0].Trim();
                             string value = assignmentElements[1].Trim();
                             switch (name)
                             {
                                 case "Mode": Enum.TryParse<BindingMode>(value, true, out bp.Mode); break;
                                 case "ItemsSource": bp.ListPropertyName = value; break;
                                 case "ItemTemplate": bp.ListItemTemplateName = value; break;
                                 case "AddCommand": bp.ListAddItemCommandName = value; break;
                                 case "RemoveCommand": bp.ListRemoveItemCommandName = value; break;
                                 case "CanEdit": bp.ListCanEditItem = value; break;
                                 case "CanMove": bp.ListCanMoveItem = value; break;
                                 // TODO: case "ItemValueId": itemValueId = value; break;
                                 // TODO: case "ListId":
                                     // commandParameterListId = AndroidHelpers.FindResourceId(value);
                                     // if (commandParameterSelectedItemAdapterView == null && commandParameterListId.HasValue) commandParameterSelectedItemAdapterView = rootView.FindViewById<AdapterView>(commandParameterListId.Value);
                                     // break;
                                 default: throw new ArgumentException("Unknown tag binding parameter: " + name);
                             }
                         }
                     }
                 }
             }
         }
     }
     return bp;
 }
示例#16
0
        /// <summary>
        /// Call InitializeBindings() in the ViewDidLoad method of a derived view class to create the data bindings and update the view with the current view model values.
        /// </summary>
        /// <param name="rootView">The view that should display the viewModel</param>
        /// <param name="viewModel">The view model</param>
        /// <param name="bindingsParameters">Optional binding parameters; use to override default parameter values for specific bindings, or as an alternative for specifying binding parameters in the view tag attribute in AXML. Note that any binding parameters specified in the tag attribute wil override bindingsParameters.</param>
        /// <param name="idPrefix">The name prefix used to match view Id to property name. Default value is the root view class name + "_"</param>
        protected void InitializeBindings(UIView rootView, ViewModelBase viewModel, BindingParameters[] bindingsParameters = null, string idPrefix = null)
        {
            Bindings = new ViewDataBindings(rootView, viewModel, idPrefix ?? this.GetType().Name + "_", this);
            this.viewModel = viewModel;

            EnsureHandlersAreAdded();

            Bindings.AddBindings(bindingsParameters); // First add any bindings that were specified in code
            // TODO: Bindings.EnsureCommandBindings();  // Then add any command bindings that were not specified in code (based on the Id naming convention)

            List<BindingParameters> bindingParametersList;
            if (ViewDataBindings.RootViewBindingParameters.TryGetValue(rootView, out bindingParametersList))
            {
                Console.WriteLine("Adding bindings from markup ...");
                ViewDataBindings.RootViewBindingParameters.Remove(rootView); // Remove the static reference to the views to prevent memory leaks. Note that if we would want to recreate the bindings later, we could also store the parameters list in the bindings.
                Bindings.AddBindings(bindingParametersList.ToArray());
            }
        }
示例#17
0
        public static BindingParameters ParseBindingParameters(string tag, BindingParameters bp = null)
        {
            if (tag != null && tag.Contains("{"))
            {
                var match = Regex.Match(tag, @"({Binding\s+((?<assignment>[^,{}]+),?)+\s*})?(\s*{List\s+((?<assignment>[^,{}]+),?)+\s*})?(\s*{CommandParameter\s+((?<assignment>[^,{}]+),?)+\s*})?");
                if (match.Success)
                {
                    var gc = match.Groups["assignment"];
                    if (gc != null)
                    {
                        var cc = gc.Captures;
                        if (cc != null)
                        {
                            if (bp == null)
                            {
                                bp = new BindingParameters();
                            }
                            for (int i = 0; i < cc.Count; i++)
                            {
                                string[] assignmentElements = cc[i].Value.Split('=');
                                if (assignmentElements.Length == 1)
                                {
                                    string value = assignmentElements[0].Trim();
                                    if (value != "")
                                    {
                                        bp.ViewModelPropertyName = value;
                                    }
                                }
                                else if (assignmentElements.Length == 2)
                                {
                                    string name  = assignmentElements[0].Trim();
                                    string value = assignmentElements[1].Trim();
                                    if (name.StartsWith("."))
                                    {
                                        bp.ViewMemberName = name.Substring(1);
                                        if (value != "")
                                        {
                                            bp.ViewModelPropertyName = value;
                                        }
                                    }
                                    else
                                    {
                                        switch (name)
                                        {
                                        case "Mode": Enum.TryParse <BindingMode>(value, true, out bp.Mode); break;

                                        case "ItemsSource": bp.ListPropertyName = value; break;

                                        case "ItemIsValue": Boolean.TryParse(value, out bp.ListItemIsValue); break;

                                        case "ItemTemplate": bp.ListItemTemplateName = value; break;

                                        case "ItemValueId": bp.ListItemValueId = value; break;

                                        case "ListId": bp.ListId = AndroidHelpers.FindResourceId(value); break;

                                        default: throw new ArgumentException("Unknown tag binding parameter: " + name);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(bp);
        }