public BindingContext(object view, string title, Theme currentTheme) { if (view == null) throw new ArgumentNullException("view"); BuildElementPropertyMap(); Root = new RootElement(title); Root.ViewBinding.DataContext = view; var themeable = Root as IThemeable; if (themeable != null) themeable.Theme = Theme.CreateTheme(currentTheme); Populate(view, Root); var viewContext = view as IBindingContext; if (viewContext != null) { viewContext.BindingContext = this; var dataContext = view as IDataContext; if (dataContext != null) { var vmContext = dataContext.DataContext as IBindingContext; if (vmContext != null) { vmContext.BindingContext = this; } } } }
public BindingContext(UIView view, string title, Theme currentTheme) { if (view == null) throw new ArgumentNullException("view"); var parser = new ViewParser(); parser.Parse(view, title, currentTheme); Root = parser.Root; var viewContext = view as IBindingContext; if (viewContext != null) { viewContext.BindingContext = this; var dataContext = view as IDataContext; if (dataContext != null) { var vmContext = dataContext.DataContext as IBindingContext; if (vmContext != null) { vmContext.BindingContext = this; } } } if (view is IView) { ((IView)view).TableView = Root.TableView; } }
public void Parse(UIView view, string caption, Theme theme) { var vw = view as IView; if (vw != null) { var captionAttribute = view.GetType().GetCustomAttribute<CaptionAttribute>(); if (captionAttribute != null) caption = captionAttribute.Caption; } Root = new RootElement(caption) { Opaque = false }; Root.DataContext = view; var dataContext = view as IDataContext; if (dataContext != null) Root.DataContext = dataContext.DataContext; var themeable = Root as IThemeable; if (themeable != null) themeable.Theme = Theme.CreateTheme(theme); var searchbarAttribute = view.GetType().GetCustomAttribute<SearchbarAttribute>(); var searchbar = Root as ISearchBar; if (searchbarAttribute != null && searchbar != null) { searchbar.SearchPlaceholder = searchbarAttribute.Placeholder; searchbar.IncrementalSearch = searchbarAttribute.IncrementalSearch; searchbar.EnableSearch = true; searchbar.IsSearchbarHidden = false; } var sectionList = CreateSectionList(view, Root); sectionList.ForEach((section)=>section.BeginInit()); Root.Add(sectionList); Root.ToolbarButtons = CheckForToolbarItems(view); Root.NavbarButtons = CheckForNavbarItems(view); var notifyDataContextChanged = view as INotifyDataContextChanged; if (notifyDataContextChanged != null) { notifyDataContextChanged.DataContextChanged += HandleNotifyDataContextChangedDataContextChanged; } }
public static void ApplyElementTheme(Theme theme, IThemeable element, MemberInfo member) { if (theme != null) { var newTheme = Theme.CreateTheme(theme); newTheme.MergeTheme(element.Theme); element.Theme = newTheme; } if (member != null) ApplyMemberTheme(member, element); if (element is IRoot) { foreach(var section in ((IRoot)element).Sections) foreach(var e in section.Elements) ApplyElementTheme(element.Theme, e, null); } if (element is ISection) foreach(var e in ((ISection)element).Elements) ApplyElementTheme(element.Theme, e, null); }
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; }
private ISection CreateEnumSection(Theme theme, MemberInfo member, IEnumerable values, object currentValue, bool popOnSelection, List<Binding> bindings) { var csection = new Section() { Opaque = false }; int index = 0; int selected = -1; foreach(var value in values) { if (currentValue != null && currentValue.Equals(value)) selected = index; var description = value.ToString(); if (value.GetType().IsEnum) description = ((Enum)value).GetDescription(); var radioElement = new RadioElement(description) { Item = value }; radioElement.Index = index; radioElement.PopOnSelect = popOnSelection; radioElement.DataContext = selected == index; radioElement.Opaque = false; csection.Add(radioElement); index++; } csection.ViewBinding.DataContextCode = DataContextCode.Enum; return csection; }
private ISection GetSectionElementForMember(Theme theme, object view, MemberInfo member, List<Binding> bindings) { var caption = GetCaption(member); Type memberType = GetTypeForMember(member); ISection section = null; var isMultiselect = member.GetCustomAttribute<MultiSelectionAttribute>() != null; var isSelect = member.GetCustomAttribute<SelectionAttribute>() != null; if (memberType.IsEnum) { SetDefaultConverter(view, member, "DataContext", new EnumConverter(), memberType, bindings); var pop = member.GetCustomAttribute<PopOnSelectionAttribute>() != null; var currentValue = member.GetValue(view); var enumValues = Enum.GetValues(memberType); section = CreateEnumSection(theme, member, enumValues, currentValue, pop, bindings); } else if (typeof(EnumCollection).IsAssignableFrom(memberType)) { section = CreateEnumCollectionSection(member, caption, view, bindings); } else if (isMultiselect) { section = CreateMultiselectCollectionSection(member, caption, view, bindings); } else if (isSelect) { section = CreateSelectCollectionSection(member, caption, view, bindings); } return section; }
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 IElement GetElementForMember(Theme theme, UIView view, MemberInfo member, List<Binding> bindings) { string caption = GetCaption(member); IElement element = null; var orderAttribute = member.GetCustomAttribute<OrderAttribute>(); 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); } } } // get a single element if(_ElementPropertyMap.ContainsKey(memberType)) { element = _ElementPropertyMap[memberType](member, caption, view, bindings); } if (typeof(IElement).IsAssignableFrom(memberType)) { var memberValue = member.GetValue(view) as IElement; if (memberValue == null) { memberValue = Activator.CreateInstance(memberType) as IElement; } if (memberValue != null) { memberValue.Caption = caption; } element = memberValue; } if (element == null) { element = GetRootElementForMember(theme, view, member, bindings); } if (orderAttribute != null && element != null) element.Order = orderAttribute.Order; var dataContext = view as IDataContext; if (dataContext != null && element.DataContext == null) { element.DataContext = dataContext.DataContext; } return element; }
public void MergeTheme(Theme theme) { if (theme != null) { Name = theme.Name; if (theme.CellStyle != UITableViewCellStyle.Default && CellStyle != theme.CellStyle) CellStyle = theme.CellStyle; if (theme.Accessory != Accessory) Accessory = theme.Accessory; // if (theme.CellImageIcon != null) // CellImageIcon = theme.CellImageIcon; // // if (theme.CellImageIconUri != null) // CellImageIconUri = theme.CellImageIconUri; if (theme.CellBackgroundColor != null) CellBackgroundColor = theme.CellBackgroundColor; if (theme.CellBackgroundUri != null) CellBackgroundUri = theme.CellBackgroundUri; if (theme.CellBackgroundImage != null) CellBackgroundImage = theme.CellBackgroundImage; if (theme.TextLabel != null) TextLabel = theme.TextLabel; if (theme.TextFont != null) TextFont = theme.TextFont; if (theme.TextColor != null) TextColor = theme.TextColor; if (theme.TextShadowOffset != SizeF.Empty) TextShadowOffset = theme.TextShadowOffset; if (theme.TextShadowColor != null) TextShadowColor = theme.TextShadowColor; if (theme.TextHighlightColor != null) TextHighlightColor = theme.TextHighlightColor; TextAlignment = theme.TextAlignment; if (theme.DetailTextLabel != null) DetailTextLabel = theme.DetailTextLabel; if (theme.DetailTextFont != null) DetailTextFont = theme.DetailTextFont; if (theme.DetailTextColor != null) DetailTextColor = theme.DetailTextColor; if (theme.DetailTextShadowOffset != SizeF.Empty) TextShadowOffset = theme.DetailTextShadowOffset; if (theme.DetailTextShadowColor != null) TextShadowColor = theme.DetailTextShadowColor; if (theme.DetailTextHighlightColor != null) DetailTextHighlightColor = theme.DetailTextHighlightColor; DetailTextAlignment = theme.DetailTextAlignment; if (theme.PlaceholderColor != null) PlaceholderColor = theme.PlaceholderColor; if (theme.PlaceholderAlignment != UITextAlignment.Right) PlaceholderAlignment = theme.PlaceholderAlignment; if (theme.SeparatorColor != null) SeparatorColor = theme.SeparatorColor; if (theme.SeparatorStyle.HasValue) SeparatorStyle = theme.SeparatorStyle; if (theme.TableViewStyle.HasValue) TableViewStyle = theme.TableViewStyle; if (theme.BarStyle.HasValue) BarStyle = theme.BarStyle; if (theme.BarTintColor != null) BarTintColor = theme.BarTintColor; if (theme.BarImage != null) BarImage = theme.BarImage; if (theme.BarTranslucent) BarTranslucent = theme.BarTranslucent; HeaderTextAlignment = theme.HeaderTextAlignment; if (theme.HeaderTextFont != null) HeaderTextFont = theme.HeaderTextFont; if (theme.HeaderTextColor != null) HeaderTextColor = theme.HeaderTextColor; if (theme.HeaderTextShadowOffset != SizeF.Empty) HeaderTextShadowOffset = theme.HeaderTextShadowOffset; if (theme.HeaderTextShadowColor != null) HeaderTextShadowColor = theme.HeaderTextShadowColor; FooterTextAlignment = theme.FooterTextAlignment; if (theme.FooterTextFont != null) FooterTextFont = theme.FooterTextFont; if (theme.FooterTextColor != null) FooterTextColor = theme.FooterTextColor; if (theme.FooterTextShadowOffset != SizeF.Empty) FooterTextShadowOffset = theme.FooterTextShadowOffset; if (theme.FooterTextShadowColor != null) FooterTextShadowColor = theme.FooterTextShadowColor; if (theme.DrawContentViewAction != null) { DrawContentViewAction = theme.DrawContentViewAction; } if (theme.BackgroundColor != null) BackgroundColor = theme.BackgroundColor; if (theme.BackgroundUri != null) BackgroundUri = theme.BackgroundUri; if (theme.BackgroundImage != null) BackgroundImage = theme.BackgroundImage; } }
public static Theme CreateTheme(Theme theme) { var newTheme = new Theme(); newTheme.MergeTheme(theme); return newTheme; }
public void MergeTheme(Theme theme) { if (theme != null) { Name = theme.Name; if (theme.CellStyle != UITableViewCellStyle.Default && CellStyle != theme.CellStyle) { CellStyle = theme.CellStyle; } if (theme.Accessory != Accessory) { Accessory = theme.Accessory; } // if (theme.CellImageIcon != null) // CellImageIcon = theme.CellImageIcon; // // if (theme.CellImageIconUri != null) // CellImageIconUri = theme.CellImageIconUri; if (theme.CellBackgroundColor != null) { CellBackgroundColor = theme.CellBackgroundColor; } if (theme.CellBackgroundUri != null) { CellBackgroundUri = theme.CellBackgroundUri; } if (theme.CellBackgroundImage != null) { CellBackgroundImage = theme.CellBackgroundImage; } if (theme.TextLabel != null) { TextLabel = theme.TextLabel; } if (theme.TextFont != null) { TextFont = theme.TextFont; } if (theme.TextColor != null) { TextColor = theme.TextColor; } if (theme.TextShadowOffset != SizeF.Empty) { TextShadowOffset = theme.TextShadowOffset; } if (theme.TextShadowColor != null) { TextShadowColor = theme.TextShadowColor; } if (theme.TextHighlightColor != null) { TextHighlightColor = theme.TextHighlightColor; } TextAlignment = theme.TextAlignment; if (theme.DetailTextLabel != null) { DetailTextLabel = theme.DetailTextLabel; } if (theme.DetailTextFont != null) { DetailTextFont = theme.DetailTextFont; } if (theme.DetailTextColor != null) { DetailTextColor = theme.DetailTextColor; } if (theme.DetailTextShadowOffset != SizeF.Empty) { TextShadowOffset = theme.DetailTextShadowOffset; } if (theme.DetailTextShadowColor != null) { TextShadowColor = theme.DetailTextShadowColor; } if (theme.DetailTextHighlightColor != null) { DetailTextHighlightColor = theme.DetailTextHighlightColor; } DetailTextAlignment = theme.DetailTextAlignment; if (theme.PlaceholderColor != null) { PlaceholderColor = theme.PlaceholderColor; } if (theme.PlaceholderAlignment != UITextAlignment.Right) { PlaceholderAlignment = theme.PlaceholderAlignment; } if (theme.SeparatorColor != null) { SeparatorColor = theme.SeparatorColor; } if (theme.SeparatorStyle.HasValue) { SeparatorStyle = theme.SeparatorStyle; } if (theme.TableViewStyle.HasValue) { TableViewStyle = theme.TableViewStyle; } if (theme.BarStyle.HasValue) { BarStyle = theme.BarStyle; } if (theme.BarTintColor != null) { BarTintColor = theme.BarTintColor; } if (theme.BarImage != null) { BarImage = theme.BarImage; } if (theme.BarTranslucent) { BarTranslucent = theme.BarTranslucent; } HeaderTextAlignment = theme.HeaderTextAlignment; if (theme.HeaderTextFont != null) { HeaderTextFont = theme.HeaderTextFont; } if (theme.HeaderTextColor != null) { HeaderTextColor = theme.HeaderTextColor; } if (theme.HeaderTextShadowOffset != SizeF.Empty) { HeaderTextShadowOffset = theme.HeaderTextShadowOffset; } if (theme.HeaderTextShadowColor != null) { HeaderTextShadowColor = theme.HeaderTextShadowColor; } FooterTextAlignment = theme.FooterTextAlignment; if (theme.FooterTextFont != null) { FooterTextFont = theme.FooterTextFont; } if (theme.FooterTextColor != null) { FooterTextColor = theme.FooterTextColor; } if (theme.FooterTextShadowOffset != SizeF.Empty) { FooterTextShadowOffset = theme.FooterTextShadowOffset; } if (theme.FooterTextShadowColor != null) { FooterTextShadowColor = theme.FooterTextShadowColor; } if (theme.DrawContentViewAction != null) { DrawContentViewAction = theme.DrawContentViewAction; } if (theme.BackgroundColor != null) { BackgroundColor = theme.BackgroundColor; } if (theme.BackgroundUri != null) { BackgroundUri = theme.BackgroundUri; } if (theme.BackgroundImage != null) { BackgroundImage = theme.BackgroundImage; } } }
private static void ApplyElementTheme(Theme theme, IThemeable element, MemberInfo member) { if (theme != null) { var newTheme = Theme.CreateTheme(theme); newTheme.MergeTheme(element.Theme); element.Theme = newTheme; } if (member != null) ApplyMemberTheme(member, element); }