Example #1
0
        private List <ISection> CreateSectionList(UIView view, IRoot root)
        {
            var memberFuncMap = new List <Func <object, MemberInfo[]> >()
            {
                (T) => GetFields(T),
                (T) => GetProperties(T),
                (T) => GetMethods(T)
            };

            ISection lastSection = new Section()
            {
                Order = -1, Parent = root as IElement
            };

            var sectionList = new List <ISection>()
            {
                lastSection
            };

            IElement newElement = null;
            Theme    theme      = null;
            var      themeable  = root as IThemeable;

            if (themeable != null)
            {
                ThemeHelper.ApplyRootTheme(view, themeable);
                theme = themeable.Theme;
                ThemeHelper.ApplyElementTheme(theme, lastSection, null);
            }

            if (!(view is IView))
            {
                newElement = new UIViewElement(null, view, false);
                lastSection.Add(newElement);
            }
            else
            {
                foreach (var memberFunc in memberFuncMap)
                {
                    var members = memberFunc(view);

                    foreach (var member in members)
                    {
                        var isList = member.GetCustomAttribute <ListAttribute>() != null;

                        var pullToRefreshAttribute = member.GetCustomAttribute <PullToRefreshAttribute>();
                        if (pullToRefreshAttribute != null)
                        {
                            root.PullToRefreshCommand = GetCommandForMember(view, member);
                            root.DefaultSettingsKey   = pullToRefreshAttribute.SettingsKey;
                        }
                        var skipAttribute = member.GetCustomAttribute <SkipAttribute>(true);
                        if (skipAttribute != null)
                        {
                            continue;
                        }

                        var sectionAttribute = member.GetCustomAttribute <SectionAttribute>();

                        if (sectionAttribute != null)
                        {
                            Theme sectionTheme = null;
                            if (sectionAttribute.ThemeType != null)
                            {
                                sectionTheme = Activator.CreateInstance(sectionAttribute.ThemeType) as Theme;
                            }

                            lastSection = new Section(sectionAttribute.Caption, sectionAttribute.Footer)
                            {
                                Order = sectionAttribute.Order
                            };
                            lastSection.Parent = root as IElement;

                            ThemeHelper.ApplyElementTheme(root.Theme, lastSection, null);
                            ThemeHelper.ApplyElementTheme(sectionTheme, lastSection, null);

                            sectionList.Add(lastSection);
                        }

                        var bindings = GetBindings(view, member);

                        newElement = GetElementForMember(root.Theme, view, member, bindings);

                        ThemeHelper.ApplyElementTheme(theme, newElement, member);

                        IBindable bindable = null;

                        if (newElement is ISection)
                        {
                            lastSection.Add(((ISection)newElement).Elements);
                            bindable = lastSection as IBindable;
                        }
                        else if (newElement != null)
                        {
                            bindable = newElement as IBindable;

                            if ((isList) && newElement is IRoot)
                            {
                                var sections = ((IRoot)newElement).Sections;

                                var firstSection = sections.FirstOrDefault();
                                if (firstSection.Elements.Count > 0)
                                {
                                    lastSection.Add(firstSection.Elements);
                                }

                                for (var index = 1; index < sections.Count; index++)
                                {
                                    sectionList.Add(sections[index]);
                                }
                            }
                            else
                            {
                                lastSection.Add(newElement);
                            }
                        }

                        if (bindable != null && bindable != _NoElement && bindings.Count != 0)
                        {
                            foreach (Binding binding in bindings)
                            {
                                if (binding.TargetPath == null)
                                {
                                    binding.TargetPath = "DataContext";
                                }

                                BindingOperations.SetBinding(bindable, binding.TargetPath, binding);
                            }
                        }
                    }
                }
            }

            foreach (var section in sectionList)
            {
                var orderedList = section.Elements.OrderBy(e => e.Order).Where((e) => e != _NoElement).ToList();
                section.Elements = new System.Collections.ObjectModel.ObservableCollection <MonoMobile.MVVM.IElement>(orderedList);
            }

            var orderedSections = sectionList.Where(s => s.Elements.Count > 0).OrderBy(section => section.Order).ToList();

            return(orderedSections);
        }
Example #2
0
        private static IRoot CreateViewEnumerableRoot(IRoot root)
        {
            IRoot newRoot = null;

            Type elementType = root.ViewBinding.ElementType;

            if (elementType == null)
            {
                elementType = typeof(RootElement);
            }

            newRoot = new RootElement()
            {
                Opaque = false, ViewBinding = root.ViewBinding
            };
            IElement element = null;

            var items   = (IEnumerable)root.DataContext;
            var section = new Section()
            {
                Opaque = false, ViewBinding = root.ViewBinding, Parent = newRoot as IElement
            };

            newRoot.Sections.Add(section);

            foreach (var e in items)
            {
                var caption = e.ToString();
                if (string.IsNullOrEmpty(caption))
                {
                    caption = root.ViewBinding.ViewType.Name.Capitalize();
                }

                element = Activator.CreateInstance(elementType) as IElement;
                ((RootElement)element).Opaque = false;
                element.Caption = caption;
                element.ViewBinding.DataContextCode = DataContextCode.Object;
                element.ViewBinding.ViewType        = root.ViewBinding.ViewType;
                element.ViewBinding.MemberInfo      = root.ViewBinding.MemberInfo;

                if (e is UIView)
                {
                    element.ViewBinding.View = e as UIView;
                }
                else
                {
                    element.DataContext = e;
                }

                if (element.ViewBinding.ViewType == null)
                {
                    element.ViewBinding.ViewType = e.GetType();
                }

                section.Add(element);
            }

            ThemeHelper.ApplyElementTheme(root.Theme, newRoot, null);

            return(newRoot);
        }