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 = MakeCaption(root.ViewBinding.ViewType.Name); } 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; }
private IElement GetRootElementForMember(Theme theme, UIView view, MemberInfo member, List<Binding> bindings) { var memberType = GetTypeForMember(member); var caption = GetCaption(member); IElement root = null; Type viewType = memberType; Type elementType = null; var genericType = memberType.GetGenericArguments().FirstOrDefault(); if (genericType != null) viewType = genericType; var listAttribute = member.GetCustomAttribute<ListAttribute>(); if (listAttribute != null && listAttribute.ViewType != null) { viewType = listAttribute.ViewType; elementType = listAttribute.ElementType; } var rootAttribute = member.GetCustomAttribute<RootAttribute>(); if (rootAttribute != null && rootAttribute.ViewType != null) { viewType = rootAttribute.ViewType; elementType = rootAttribute.ElementType; } var isEnum = memberType.IsEnum; var isEnumCollection = typeof(EnumCollection).IsAssignableFrom(memberType); var isMultiselect = member.GetCustomAttribute<MultiSelectionAttribute>() != null; var isSelect = member.GetCustomAttribute<SelectionAttribute>() != null; var isView = typeof(IView).IsAssignableFrom(memberType) || typeof(IView).IsAssignableFrom(viewType); var isUIView = typeof(UIView).IsAssignableFrom(memberType) || typeof(UIView).IsAssignableFrom(viewType); var isEnumerable = typeof(IEnumerable).IsAssignableFrom(memberType) && !(isView || isUIView); var isList = member.GetCustomAttribute<ListAttribute>() != null; if (isEnum || isEnumCollection || isMultiselect || isSelect) { ISection section = GetSectionElementForMember(theme, view, member, bindings); if (!isList && section != null) { var rootElement = new RootElement() { section }; rootElement.Caption = caption; rootElement.Opaque = false; rootElement.Theme = Theme.CreateTheme(Root.Theme); rootElement.ViewBinding = section.ViewBinding; rootElement.Theme.CellStyle = GetCellStyle(member, UITableViewCellStyle.Value1); root = rootElement; } else { root = section as IElement; } } else if (isEnumerable) { var rootElement = CreateEnumerableRoot(theme, member, caption, view, bindings); if (isList) { root = rootElement.Sections.FirstOrDefault() as IElement; } else { root = rootElement as IElement; } } else if (isView || isUIView) { object dataContext = view; MemberInfo dataContextMember = GetMemberFromDataContext(member, ref dataContext); var items = dataContextMember.GetValue(dataContext); var rootElement = new RootElement(caption) { Opaque = false }; rootElement.Theme = Theme.CreateTheme(Root.Theme); rootElement.ViewBinding.MemberInfo = dataContextMember; rootElement.ViewBinding.ElementType = elementType; rootElement.ViewBinding.ViewType = viewType; rootElement.ViewBinding.DataContextCode = DataContextCode.Object; rootElement.DataContext = dataContext; rootElement.Theme.CellStyle = GetCellStyle(member, UITableViewCellStyle.Default); if (items != null) { if (items is UIView) { rootElement.ViewBinding.View = items as UIView; } else { rootElement.DataContext = items; } } if (genericType != null) { SetDefaultConverter(view, member, "DataContext", new EnumerableConverter(), null, bindings); rootElement.ViewBinding.DataContextCode = DataContextCode.ViewEnumerable; rootElement.ViewBinding.ViewType = viewType; } if (isList) { var innerRoot = BindingContext.CreateRootedView(rootElement); root = innerRoot as IElement; } else { root = rootElement; } } else { throw new Exception(string.Format("Unknown type ({0}). Are you missing a [Root] or [List] attribute?", memberType)); } SetDefaultConverter(view, member, "DataContext", new ViewConverter(), null, bindings); return root; }
private IRoot CreateEnumerableRoot(Theme theme, MemberInfo member, string caption, object view, List<Binding> bindings) { var rootAttribute = member.GetCustomAttribute<RootAttribute>(); var listAttribute = member.GetCustomAttribute<ListAttribute>(); var viewAttribute = member.GetCustomAttribute<ViewAttribute>(); Type elementType = listAttribute.ElementType; if (elementType == null) { elementType = rootAttribute.ElementType; } SetDefaultConverter(view, member, "DataContext", new EnumerableConverter(), null, bindings); var items = (IEnumerable)member.GetValue(view); if (items == null) throw new ArgumentNullException(member.Name, string.Format("Member of class {1} must have a value.", view.GetType().Name)); var genericType = items.GetType().GetGenericArguments().SingleOrDefault(); var isUIView = typeof(UIView).IsAssignableFrom(genericType); var section = CreateEnumSection(theme, member, items, null, true, bindings); var root = new RootElement(caption) { section }; root.Opaque = false; root.ViewBinding.MemberInfo = member; root.DataContext = items; root.ViewBinding.DataContextCode = DataContextCode.Enumerable; if (isUIView) { root.ViewBinding.ViewType = genericType; root.ViewBinding.DataContextCode = DataContextCode.ViewEnumerable; } else if (viewAttribute != null && viewAttribute.ViewType != null) { root.ViewBinding.ViewType = viewAttribute.ViewType; root.ViewBinding.DataContextCode = DataContextCode.ViewEnumerable; } if (rootAttribute != null && rootAttribute.ViewType != null) { root.ViewBinding.ViewType = rootAttribute.ViewType; root.ViewBinding.DataContextCode = DataContextCode.ViewEnumerable; } if (listAttribute != null) { root.ViewBinding.ViewType = listAttribute.ViewType ?? root.ViewBinding.ViewType; } root.Theme.CellStyle = GetCellStyle(member, UITableViewCellStyle.Value1); return root; }
public void Selected(DialogViewController dvc, UITableView tableView, NSIndexPath path) { BindingContext bindingContext = null; if (ViewBinding != null) { switch (ViewBinding.DataContextCode) { case DataContextCode.Object: { object view = ViewBinding.CurrentView; if (view == null) { if (ViewBinding.DataContext != null && ViewBinding.DataContext is UIView) { view = ViewBinding.DataContext as UIView; } else if (ViewBinding.ViewType != null) { view = Activator.CreateInstance(ViewBinding.ViewType); var dataContext = view as IDataContext; if (dataContext != null) { if (dataContext.DataContext == null) dataContext.DataContext = ViewBinding.DataContext; var lifetime = dataContext.DataContext as ISupportInitialize; if (lifetime != null) lifetime.BeginInit(); lifetime = view as ISupportInitialize; if (lifetime != null) lifetime.BeginInit(); } } } bindingContext = new BindingContext(view, Caption, Root.Theme); var root = (RootElement)bindingContext.Root; root.ActivateController(dvc, tableView, path); return; } case DataContextCode.Enum: break; case DataContextCode.EnumCollection: break; case DataContextCode.Enumerable: { Sections.Clear(); IElement element = null; var items = (IEnumerable)ViewBinding.DataContext; var genericType = items.GetType().GetGenericArguments().SingleOrDefault(); if (genericType is IView) { Sections.Add(new Section()); foreach (var e in items) { element = new RootElement(e.ToString()) { ViewBinding = ViewBinding, Theme = Theme }; element.ViewBinding.DataContextCode = DataContextCode.Object; Sections[0].Add(element); } } else { var section = BindingContext.CreateEnumSection(this, items, null, true); Sections.Add(section); } break; } } } ActivateController(dvc, tableView, path); }
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); }
public IElement CreateEnumerableRoot(MemberInfo member, string caption, object view, List<Binding> bindings) { SetDefaultConverter(member, "Value", new EnumerableConverter(), bindings); var rootAttribute = member.GetCustomAttribute<RootAttribute>(); var listAttribute = member.GetCustomAttribute<ListAttribute>(); var items = (IEnumerable)GetValue(member, view); var element = new RootElement(caption); element.ViewBinding.DataContext = view; element.ViewBinding.MemberInfo = member; element.ViewBinding.DataContext = items; element.ViewBinding.DataContextCode = DataContextCode.Enumerable; var dataTemplate = view as IDataTemplate; if (dataTemplate != null) { rootAttribute = dataTemplate.CustomAttributes.FirstOrDefault((o)=>o.GetType() == typeof(RootAttribute)) as RootAttribute; listAttribute = dataTemplate.CustomAttributes.FirstOrDefault((o)=>o.GetType() == typeof(ListAttribute)) as ListAttribute; } if (rootAttribute != null) { element.ViewBinding.ViewType = rootAttribute.ViewType; } if (listAttribute != null) { element.ViewBinding.ViewType = listAttribute.ViewType ?? element.ViewBinding.ViewType; } if (element.ViewBinding.ViewType != null) { dataTemplate = new StandardListView() { Items = items }; List<object> customAttributes = new List<object>(member.GetCustomAttributes(false)); var propertyInfo = dataTemplate.GetType().GetProperty("Items"); var itemAttributes = propertyInfo.GetCustomAttributes(false); customAttributes.AddRange(itemAttributes); dataTemplate.CustomAttributes = customAttributes; element.ViewBinding.DataContext = dataTemplate; element.ViewBinding.DataContextCode = DataContextCode.Object; element.ViewBinding.MemberInfo = propertyInfo; } element.Theme.CellStyle = GetCellStyle(member, UITableViewCellStyle.Default); return element; }
public IElement CreateEnumCollectionRoot(MemberInfo member, string caption, object view, List<Binding> bindings) { Type memberType = GetTypeForMember(member); SetDefaultConverter(member, "Value", new EnumItemsConverter(), bindings); var csection = new Section() { IsMultiselect = true, Opaque = false }; ApplyRootTheme(view, csection); var collection = GetValue(member, view); if (collection == null) { var collectionType = typeof(EnumCollection<>); var enumType = memberType.GetGenericArguments()[0]; Type[] generic = { enumType }; collection = Activator.CreateInstance(collectionType.MakeGenericType(generic)); (member as PropertyInfo).SetValue(view, collection, new object[] {}); } var index = 0; var items = (EnumCollection)collection; foreach (var item in items.AllValues) { var checkboxElement = new CheckboxElement(item.Description) { Index = index, Value = item.IsChecked, Group = item.GroupName}; ApplyRootTheme(view, checkboxElement); csection.Add(checkboxElement); index++; } var element = new RootElement(caption) { csection }; element.ViewBinding.DataContextCode = DataContextCode.EnumCollection; element.Theme.CellStyle = UITableViewCellStyle.Value1; return element; }
public IElement CreateEnumRoot(MemberInfo member, string caption, object view, List<Binding> bindings) { Type memberType = GetTypeForMember(member); var currentValue = (int)member.GetValue(view); SetDefaultConverter(member, "Value", new EnumConverter() { PropertyType = memberType }, bindings); var pop = member.GetCustomAttribute<PopOnSelectionAttribute>() != null; var enumValues = Enum.GetValues(memberType); var csection = CreateEnumSection(Root, enumValues, currentValue, pop); var element = new RootElement(caption) { csection }; element.Caption = caption; element.ViewBinding.DataContext = memberType; element.ViewBinding.DataContextCode = DataContextCode.Enum; element.Opaque = false; element.Theme = Theme.CreateTheme(Root.Theme); element.Theme.CellStyle = UITableViewCellStyle.Value1; var radioGroup = new RadioGroup(memberType.FullName, currentValue) { EnumType = memberType }; ((IRoot)element).Groups.Add(radioGroup); return element; }
private IElement GetElementForMember(object view, MemberInfo member) { string caption = null; IElement element = null; var bindings = new List<Binding>(); var captionAttribute = member.GetCustomAttribute<CaptionAttribute>(); var orderAttribute = member.GetCustomAttribute<OrderAttribute>(); var bindAttributes = member.GetCustomAttributes(typeof(BindAttribute), false); var memberDataContext = view; if(captionAttribute != null) caption = captionAttribute.Caption; else caption = MakeCaption(member.Name); Type memberType = GetTypeForMember(member); if (!(member is MethodInfo)) { var defaultValue = member.GetCustomAttribute<DefaultValueAttribute>(); if (defaultValue != null) { var propertyInfo = member as PropertyInfo; var fieldInfo = member as FieldInfo; if (propertyInfo != null && propertyInfo.CanWrite) { propertyInfo.SetValue(view, defaultValue.Value, null); } if (fieldInfo != null) { fieldInfo.SetValue(view, defaultValue.Value); } } foreach (BindAttribute bindAttribute in bindAttributes) { bindings.Add(bindAttribute.Binding); } var valueBinding = bindings.Where((b)=>b.TargetPath == "Value").FirstOrDefault() != null; if (!valueBinding) { bindings.Add(new Binding(member.Name, null)); } foreach(var binding in bindings) { if (string.IsNullOrEmpty(binding.SourcePath)) { binding.SourcePath = member.Name; } var sourceDataContext = memberDataContext; binding.Source = sourceDataContext; } } if(_ElementPropertyMap.ContainsKey(memberType)) { element = _ElementPropertyMap[memberType](member, caption, view, bindings); } else if (memberType.IsEnum) { element = CreateEnumRoot(member, caption, memberDataContext, bindings); } else if (typeof(EnumCollection).IsAssignableFrom(memberType)) { element = CreateEnumCollectionRoot(member, caption, memberDataContext, bindings); } else if (typeof(IEnumerable).IsAssignableFrom(memberType) && !typeof(IView).IsAssignableFrom(memberType)) { element = CreateEnumerableRoot(member, caption, memberDataContext, bindings); } else { element = new RootElement() { }; SetDefaultConverter(member, "Value", new ViewConverter(), bindings); var nested = GetValue(member, view) as UIView; if (nested != null) { element.ViewBinding.DataContext = view; element.ViewBinding.View = nested; } var viewAttribute = member.GetCustomAttribute<ViewAttribute>(); if (viewAttribute != null && viewAttribute.ViewType != null) element.ViewBinding.ViewType = viewAttribute.ViewType; else element.ViewBinding.ViewType = memberType; element.ViewBinding.MemberInfo = member; element.Caption = caption; element.Theme.CellStyle = GetCellStyle(member, UITableViewCellStyle.Default); } if (orderAttribute != null && element != null) element.Order = orderAttribute.Order; var bindable = element as IBindable; if (bindable != null && bindings.Count != 0) { foreach (Binding binding in bindings) { if (binding.TargetPath == null) { binding.TargetPath = "Value"; } BindingOperations.SetBinding(bindable, binding.TargetPath, binding); } } return element; }
private List<ISection> CreateSectionList(object 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) { ApplyRootTheme(view, themeable); theme = themeable.Theme; ApplyElementTheme(theme, lastSection, null); } foreach(var memberFunc in memberFuncMap) { var members = memberFunc(view); foreach (var member in members) { 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 inline = member.GetCustomAttribute<InlineAttribute>() != null; // var isRoot = member.GetCustomAttribute<RootAttribute>() != null; var listAttribute = member.GetCustomAttribute<ListAttribute>(); var isList = listAttribute != null; 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; ApplyElementTheme(root.Theme, lastSection, null); ApplyElementTheme(sectionTheme, lastSection, null); sectionList.Add(lastSection); } newElement = GetElementForMember(view, member); if(newElement != null) { newElement.Theme.MergeTheme(root.Theme); ApplyElementTheme(root.Theme, newElement, member); //var context = newElement as IView; // var displayInline = (inline || !isRoot) && newElement is IRoot; //&& context != null; if (isList) { var newRoot = newElement as IRoot; Type viewType = newRoot.ViewBinding.ViewType ?? listAttribute.ViewType; string caption = null; if (!(view is IDataTemplate)) caption = newElement.Caption; lastSection = new Section(caption,null) { }; lastSection.Parent = root as IElement; ApplyElementTheme(root.Theme, lastSection, null); sectionList.Add(lastSection); IEnumerable datacontext = null; if (view is IDataTemplate) datacontext = ((IDataTemplate)view).Items; else datacontext = (IEnumerable)GetValue(member, view); foreach (var e in datacontext) { IElement element = null; if (e is IViewModel) { element = new RootElement(e.ToString()); element.ViewBinding.ViewType = viewType; element.ViewBinding.DataContext = e; ((IRoot)element).Theme = Theme.CreateTheme(root.Theme); element.Theme = ((IRoot)element).Theme; if (listAttribute.ThemeType != null) { var listTheme = Activator.CreateInstance(listAttribute.ThemeType) as Theme; var rootTheme = Theme.CreateTheme(((IRoot)element).Theme); rootTheme.MergeTheme(listTheme); ((IRoot)element).Theme = rootTheme; } } else { element = new RadioElement(e.ToString()); element.Theme = ((IRoot)element).Theme; } lastSection.Add(element); } } else if (inline) { var inlineSection = new Section(string.Empty, null) { }; ApplyElementTheme(newElement.Theme, inlineSection, null); inlineSection.Parent = root as IElement; sectionList.Add(inlineSection); IRoot inlineRoot = newElement as IRoot; if (newElement.ViewBinding.DataContextCode == DataContextCode.Object) { var bindingContext = new BindingContext(newElement.ViewBinding.CurrentView, newElement.Caption, newElement.Theme); inlineRoot = bindingContext.Root; } inlineSection.Caption = newElement.Caption; foreach(var element in inlineRoot.Sections[0].Elements) inlineSection.Add(element); //root.Groups.Add(inlineRoot.Groups[0]); } else { lastSection.Add(newElement); } } } } foreach (var section in sectionList) { var orderedList = section.Elements.OrderBy(e=>e.Order).ToList(); section.Elements = orderedList; } var orderedSections = sectionList.Where(s=>s.Elements.Count > 0).OrderBy(section=>section.Order).ToList(); return orderedSections; }