コード例 #1
0
        /// <summary>
        /// Returns the current view (activity) as a list of .NET objects.
        /// </summary>
        /// <typeparam name="TViewModel">The type of the ViewModel associated to the activity.</typeparam>
        /// <param name="bindingActivity">The current activity we want to get as a list of XML elements.</param>
        /// <returns>A list of .NET objects which composed the view.</returns>
        private static List <View> GetViewAsObjects <TViewModel>(ActivityBase <TViewModel> bindingActivity) where TViewModel : IControllerBase
        {
            // Get the objects on the view
            var rootView = bindingActivity.Window.DecorView.FindViewById(Android.Resource.Id.Content);

            return(GetAllChildrenInView(rootView, true));
        }
コード例 #2
0
        /// <summary>
        /// Returns the current view (activity) as a list of XML element.
        /// </summary>
        /// <typeparam name="TViewModel">The type of the ViewModel associated to the activity.</typeparam>
        /// <param name="bindingActivity">The current activity we want to get as a list of XML elements.</param>
        /// <param name="viewLayoutResourceId">The id corresponding to the layout.</param>
        /// <returns>A list of XML elements which represent the XML layout of the view.</returns>
        private static List <XElement> GetViewAsXmlElements <TViewModel>(ActivityBase <TViewModel> bindingActivity, int viewLayoutResourceId) where TViewModel : IControllerBase
        {
            List <XElement> xmlElements;

            using (var viewAsXmlReader = bindingActivity.Resources.GetLayout(viewLayoutResourceId))
            {
                using (var sb = new StringBuilder())
                {
                    while (viewAsXmlReader.Read())
                    {
                        sb.Append(viewAsXmlReader.ReadOuterXml());
                    }

                    var viewAsXDocument = XDocument.Parse(sb.ToString());
                    xmlElements = viewAsXDocument.Descendants().ToList();
                }
            }

            return(xmlElements);
        }
コード例 #3
0
        public static IEnumerable <Binding> BindXml <TViewModel>(ActivityBase <TViewModel> bindingActivity, IControllerBase source) where TViewModel : IControllerBase
        {
            var bindings = new List <Binding>();

            List <View>     viewElements = null;
            List <XElement> xmlElements  = null;

            // Find the value of the ViewLayoutResourceId property
            var viewLayoutResourceIdProperty = bindingActivity.GetType().GetProperty(ViewLayoutResourceIdPropertyName);
            var viewLayoutResourceId         = (int)viewLayoutResourceIdProperty.GetValue(bindingActivity);

            if (viewLayoutResourceId > -1)
            {
                // Load the XML elements of the view
                xmlElements = GetViewAsXmlElements(bindingActivity, viewLayoutResourceId);
            }

            // If there is at least one 'Binding' attribute set in the XML file, get the view as objects
            if (xmlElements != null && xmlElements.Any(xe => xe.Attribute(BindingOperationXmlNamespace) != null))
            {
                viewElements = GetViewAsObjects(bindingActivity);
            }

            if (xmlElements != null && xmlElements.Any() && viewElements != null && viewElements.Any())
            {
                // Get all the binding operations inside the XML file.
                var bindingOperations = ExtractBindingOperationsFromLayoutFile(xmlElements, viewElements, source);
                if (bindingOperations != null && bindingOperations.Any())
                {
                    foreach (var bindingOperation in bindingOperations)
                    {
                        bindings.Add(BindInternal(bindingOperation, null));
                    }
                }
            }
            return(bindings);
        }