void Populate (object callbacks, object o, RootElement root) { MemberInfo last_radio_index = null; BindingFlags flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance; if (Attribute.GetCustomAttribute(o.GetType (), typeof(SkipNonPublicAttribute)) == null) flags |= BindingFlags.NonPublic; var members = o.GetType ().GetMembers (flags); Section section = null; foreach (var mi in members){ Type mType = GetTypeForMember (mi); if (mType == null) continue; string caption = null; object [] attrs = mi.GetCustomAttributes (false); bool skip = false; foreach (var attr in attrs){ if (attr is SkipAttribute) skip = true; if (attr is CaptionAttribute) caption = ((CaptionAttribute) attr).Caption; else if (attr is SectionAttribute){ if (section != null) root.Add (section); var sa = attr as SectionAttribute; section = new Section (sa.Caption, sa.Footer); } } if (skip) continue; if (caption == null) caption = MakeCaption (mi.Name); if (section == null) section = new Section (); Element element = null; if (mType == typeof (string)){ PasswordAttribute pa = null; AlignmentAttribute align = null; EntryAttribute ea = null; object html = null; NSAction invoke = null; bool multi = false; foreach (object attr in attrs){ if (attr is PasswordAttribute) pa = attr as PasswordAttribute; else if (attr is EntryAttribute) ea = attr as EntryAttribute; else if (attr is MultilineAttribute) multi = true; else if (attr is HtmlAttribute) html = attr; else if (attr is AlignmentAttribute) align = attr as AlignmentAttribute; if (attr is OnTapAttribute){ string mname = ((OnTapAttribute) attr).Method; if (callbacks == null){ throw new Exception ("Your class contains [OnTap] attributes, but you passed a null object for `context' in the constructor"); } var method = callbacks.GetType ().GetMethod (mname); if (method == null) throw new Exception ("Did not find method " + mname); invoke = delegate { method.Invoke (method.IsStatic ? null : callbacks, new object [0]); }; } } string value = (string) GetValue (mi, o); if (pa != null) element = new EntryElement (caption, pa.Placeholder, value, true); else if (ea != null) element = new EntryElement (caption, ea.Placeholder, value) { KeyboardType = ea.KeyboardType }; else if (multi) element = new MultilineElement (caption, value); else if (html != null) element = new HtmlElement (caption, value); else { var selement = new StringElement (caption, value); element = selement; if (align != null) selement.Alignment = align.Alignment; } if (invoke != null) ((StringElement) element).Tapped += invoke; } else if (mType == typeof (float)){ var floatElement = new FloatElement (null, null, (float) GetValue (mi, o)); floatElement.Caption = caption; element = floatElement; foreach (object attr in attrs){ if (attr is RangeAttribute){ var ra = attr as RangeAttribute; floatElement.MinValue = ra.Low; floatElement.MaxValue = ra.High; floatElement.ShowCaption = ra.ShowCaption; } } } else if (mType == typeof (bool)){ bool checkbox = false; foreach (object attr in attrs){ if (attr is CheckboxAttribute) checkbox = true; } if (checkbox) element = new CheckboxElement (caption, (bool) GetValue (mi, o)); else element = new BooleanElement (caption, (bool) GetValue (mi, o)); } else if (mType == typeof (DateTime)){ var dateTime = (DateTime) GetValue (mi, o); bool asDate = false, asTime = false; foreach (object attr in attrs){ if (attr is DateAttribute) asDate = true; else if (attr is TimeAttribute) asTime = true; } if (asDate) element = new DateElement (caption, dateTime); else if (asTime) element = new TimeElement (caption, dateTime); else element = new DateTimeElement (caption, dateTime); } else if (mType.IsEnum){ var csection = new Section (); ulong evalue = Convert.ToUInt64 (GetValue (mi, o), null); int idx = 0; int selected = 0; foreach (var fi in mType.GetFields (BindingFlags.Public | BindingFlags.Static)){ ulong v = Convert.ToUInt64 (GetValue (fi, null)); if (v == evalue) selected = idx; csection.Add (new RadioElement (MakeCaption (fi.Name))); idx++; } element = new RootElement (caption, new RadioGroup (null, selected)) { csection }; } else if (mType == typeof (UIImage)){ element = new ImageElement ((UIImage) GetValue (mi, o)); } else if (typeof (System.Collections.IEnumerable).IsAssignableFrom (mType)){ var csection = new Section (); int count = 0; if (last_radio_index == null) throw new Exception ("IEnumerable found, but no previous int found"); foreach (var e in (IEnumerable) GetValue (mi, o)){ csection.Add (new RadioElement (e.ToString ())); count++; } int selected = (int) GetValue (last_radio_index, o); if (selected >= count || selected < 0) selected = 0; element = new RootElement (caption, new MemberRadioGroup (null, selected, last_radio_index)) { csection }; last_radio_index = null; } else if (typeof (int) == mType){ foreach (object attr in attrs){ if (attr is RadioSelectionAttribute){ last_radio_index = mi; break; } } } else { var nested = GetValue (mi, o); if (nested != null){ var newRoot = new RootElement (caption); Populate (callbacks, nested, newRoot); element = newRoot; } } if (element == null) continue; section.Add (element); mappings [element] = new MemberAndInstance (mi, o); } root.Add (section); }
public WebViewController(HtmlElement container) : base() { this.container = container; }
private void BuildElementPropertyMap() { _ElementPropertyMap = new Dictionary<Type, Func<MemberInfo, string, object, List<Binding>, Element>>(); _ElementPropertyMap.Add(typeof(MethodInfo), (member, caption, dataContext, bindings)=> { Element element = null; var buttonAttribute = member.GetCustomAttribute<ButtonAttribute>(); if (buttonAttribute != null) { element = new ButtonElement(caption) { BackgroundColor = buttonAttribute.BackgroundColor, TextColor = buttonAttribute.TextColor, Command = GetCommandForMember(dataContext, member) }; } return element; }); _ElementPropertyMap.Add(typeof(CLLocationCoordinate2D), (member, caption, dataContext, bindings)=> { Element element = null; var mapAttribute = member.GetCustomAttribute<MapAttribute>(); var location = (CLLocationCoordinate2D)GetValue(member, dataContext); if (mapAttribute != null) element = new MapElement(mapAttribute.Caption, mapAttribute.Value, location); return element; }); _ElementPropertyMap.Add(typeof(string), (member, caption, dataContext, bindings)=> { Element element = null; var passwordAttribute = member.GetCustomAttribute<PasswordAttribute>(); var entryAttribute = member.GetCustomAttribute<EntryAttribute>(); var multilineAttribute = member.GetCustomAttribute<MultilineAttribute>(); var htmlAttribute = member.GetCustomAttribute<HtmlAttribute>(); var alignmentAttribute = member.GetCustomAttribute<AlignmentAttribute>(); if (passwordAttribute != null) element = new EntryElement(caption, passwordAttribute.Placeholder, true); else if (entryAttribute != null) element = new EntryElement(caption, entryAttribute.Placeholder) { KeyboardType = entryAttribute.KeyboardType }; else if (multilineAttribute != null) element = new MultilineElement(caption); else if (htmlAttribute != null) { SetDefaultConverter(member, "Value", new UriConverter(), bindings); element = new HtmlElement(caption); } else { var selement = new StringElement(caption, (string)GetValue(member, dataContext)); if (alignmentAttribute != null) selement.Alignment = alignmentAttribute.Alignment; element = selement; } var tappable = element as ITappable; if (tappable != null) ((ITappable)element).Command = GetCommandForMember(dataContext, member); return element; }); _ElementPropertyMap.Add(typeof(float), (member, caption, dataContext, bindings)=> { Element element = null; var rangeAttribute = member.GetCustomAttribute<RangeAttribute>(); if (rangeAttribute != null) { var floatElement = new FloatElement() { }; floatElement.Caption = caption; element = floatElement; floatElement.MinValue = rangeAttribute.Low; floatElement.MaxValue = rangeAttribute.High; floatElement.ShowCaption = rangeAttribute.ShowCaption; } else { var entryAttribute = member.GetCustomAttribute<EntryAttribute>(); var placeholder = ""; var keyboardType = UIKeyboardType.NumberPad; if(entryAttribute != null) { placeholder = entryAttribute.Placeholder; if(entryAttribute.KeyboardType != UIKeyboardType.Default) keyboardType = entryAttribute.KeyboardType; } element = new EntryElement(caption, placeholder, "") { KeyboardType = keyboardType }; } return element; }); _ElementPropertyMap.Add(typeof(Uri), (member, caption, dataContext, bindings)=> { return new HtmlElement(caption, (Uri)GetValue(member, dataContext)); }); _ElementPropertyMap.Add(typeof(bool), (member, caption, dataContext, bindings)=> { Element element = null; var checkboxAttribute = member.GetCustomAttribute<CheckboxAttribute>(); if (checkboxAttribute != null) element = new CheckboxElement(caption) { }; else element = new BooleanElement(caption) { }; return element; }); _ElementPropertyMap.Add(typeof(DateTime), (member, caption, dataContext, bindings)=> { Element element = null; var dateAttribute = member.GetCustomAttribute<DateAttribute>(); var timeAttribute = member.GetCustomAttribute<TimeAttribute>(); if(dateAttribute != null) element = new DateElement(caption); else if (timeAttribute != null) element = new TimeElement(caption); else element = new DateTimeElement(caption); return element; }); _ElementPropertyMap.Add(typeof(UIImage),(member, caption, dataContext, bindings)=> { return new ImageElement(); }); _ElementPropertyMap.Add(typeof(int), (member, caption, dataContext, bindings)=> { return new StringElement(caption) { Value = GetValue(member, dataContext).ToString() }; }); }
RootElement CreateRoot() { var e_html = new HtmlElement ("Login", URL); var e_name = new EntryElement("Blogname: ", "Enter the name of the blog", Name); var e_url = new EntryElement("URL:", "Enter the URL", URL); var e_login = new EntryElement("Login:"******"Enter your login-name", Login); e_login.Changed += delegate { Login = e_login.Value; }; var e_passwd = new EntryElement("Password:"******"Enter your password", Passwd, true); e_passwd.Changed += delegate { Passwd = e_passwd.Value; }; var e_save = new StringElement( "Save", SavePW); e_save.Alignment = UITextAlignment.Center; var e_clear = new StringElement( "Delete", Clear); e_clear.Alignment = UITextAlignment.Center; var root = new RootElement (Name, getDVC) { new Section (){ e_name, e_url, e_login, e_passwd }, new Section(){ e_save, e_html }, new Section () { e_clear } }; e_name.Changed += delegate { Name = e_name.Value; root.Caption = Name; }; e_url.Changed += delegate { URL = e_url.Value.ToLower(); if(! URL.StartsWith("http://") ) URL = URL.Insert(0, "http://"); e_html.Url = URL; if( String.IsNullOrEmpty( Name ) || Name == StartName){ Name = URL.Remove(0, 7); root.Caption = Name; e_name.Value = Name; } }; return root; }
public async override void ViewDidLoad() { base.ViewDidLoad(); if(id > 0) { BTProgressHUD.Show("Loading..."); await viewModel.Init(id); BTProgressHUD.Dismiss(); } var delete = UIButton.FromType(UIButtonType.RoundedRect); delete.SetTitle("Delete", UIControlState.Normal); delete.Frame = new System.Drawing.RectangleF(0, 0, 320, 44); delete.TouchUpInside += async (sender, e) => { await viewModel.ExecuteDeleteMovieCommand(viewModel.Id); NavigationController.PopToRootViewController(true); }; var save = UIButton.FromType(UIButtonType.RoundedRect); save.SetTitle("Save", UIControlState.Normal); save.Frame = new System.Drawing.RectangleF(0, 0, 320, 44); save.TouchUpInside += async (sender, e) => { viewModel.IsFavorite = isFavorite.Value; await viewModel.ExecuteSaveMovieCommand(); NavigationController.PopToRootViewController(true); }; this.Root = new RootElement(viewModel.Title) { new Section("Movie Details") { (title = new StringElement("Title")), (imdbId = new HtmlElement("Imdb", string.Empty)), (runtime = new StringElement("Runtime")), (releaseDate = new StringElement("Release Date")), (isFavorite = new BooleanElement("Favorite?", false)), }, new Section() { delete, save } }; title.Value = viewModel.Title; imdbId.Url = string.Format("http://m.imdb.com/title/{0}", viewModel.ImdbId); runtime.Value = viewModel.Runtime.ToString(); releaseDate.Value = viewModel.ReleaseDate.ToShortDateString(); isFavorite.Value = viewModel.IsFavorite; this.NavigationItem.RightBarButtonItem = new UIBarButtonItem(UIBarButtonSystemItem.Camera, async delegate { var picker = new Xamarin.Media.MediaPicker(); if (picker.PhotosSupported) { try { MediaFile file = await picker.PickPhotoAsync(); if (file != null) { viewModel.Image = file.GetStream(); await viewModel.ExecuteSaveMovieCommand(); NavigationController.PopToRootViewController(true); } } catch { } } }); }
void Populate(object callbacks, object o, RootElement root) { MemberInfo last_radio_index = null; var members = o.GetType().GetMembers(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ); Section section = null; foreach (var mi in members) { Type mType = GetTypeForMember(mi); if (mType == null) { continue; } string caption = null; string key = null; // l18n object [] attrs = mi.GetCustomAttributes(false); bool skip = false; bool localize = false; foreach (var attr in attrs) { if (attr is LocalizeAttribute) { localize = true; break; } } foreach (var attr in attrs) { if (attr is SkipAttribute || attr is System.Runtime.CompilerServices.CompilerGeneratedAttribute) { skip = true; } else if (attr is CaptionAttribute) { caption = ((CaptionAttribute)attr).Caption; if (localize) { caption = NSBundle.MainBundle.LocalizedString(caption, caption); } } else if (attr is SectionAttribute) { if (section != null) { root.Add(section); } var sa = attr as SectionAttribute; var header = sa.Caption; var footer = sa.Footer; if (localize) { header = NSBundle.MainBundle.LocalizedString(header, header); footer = NSBundle.MainBundle.LocalizedString(footer, footer); } section = new Section(header, footer); } } if (skip) { continue; } if (caption == null) { caption = MakeCaption(mi.Name); if (localize) { caption = NSBundle.MainBundle.LocalizedString(caption, caption); } } if (section == null) { section = new Section(); } Element element = null; if (mType == typeof(string)) { PasswordAttribute pa = null; AlignmentAttribute align = null; EntryAttribute ea = null; object html = null; Action invoke = null; bool multi = false; foreach (object attr in attrs) { if (attr is PasswordAttribute) { pa = attr as PasswordAttribute; } else if (attr is EntryAttribute) { ea = attr as EntryAttribute; } else if (attr is MultilineAttribute) { multi = true; } else if (attr is HtmlAttribute) { html = attr; } else if (attr is AlignmentAttribute) { align = attr as AlignmentAttribute; } if (attr is OnTapAttribute) { string mname = ((OnTapAttribute)attr).Method; if (callbacks == null) { throw new Exception("Your class contains [OnTap] attributes, but you passed a null object for `context' in the constructor"); } var method = callbacks.GetType().GetMethod(mname); if (method == null) { throw new Exception("Did not find method " + mname); } invoke = delegate { method.Invoke(method.IsStatic ? null : callbacks, new object [0]); }; } } string value = (string)GetValue(mi, o); if (pa != null) { var placeholder = pa.Placeholder; if (localize) { placeholder = NSBundle.MainBundle.LocalizedString(placeholder, placeholder); } element = new EntryElement(caption, placeholder, value, true); } else if (ea != null) { var placeholder = ea.Placeholder; if (localize) { placeholder = NSBundle.MainBundle.LocalizedString(placeholder, placeholder); } element = new EntryElement(caption, placeholder, value) { KeyboardType = ea.KeyboardType, AutocapitalizationType = ea.AutocapitalizationType, AutocorrectionType = ea.AutocorrectionType, ClearButtonMode = ea.ClearButtonMode }; } else if (multi) { element = new MultilineElement(caption, value); } else if (html != null) { element = new HtmlElement(caption, value); } else { var selement = new StringElement(caption, value); element = selement; if (align != null) { selement.Alignment = align.Alignment; } } if (invoke != null) { ((StringElement)element).Tapped += invoke; } } else if (mType == typeof(float)) { var floatElement = new FloatElement(null, null, (float)GetValue(mi, o)); floatElement.Caption = caption; element = floatElement; foreach (object attr in attrs) { if (attr is RangeAttribute) { var ra = attr as RangeAttribute; floatElement.MinValue = ra.Low; floatElement.MaxValue = ra.High; floatElement.ShowCaption = ra.ShowCaption; } } } else if (mType == typeof(bool)) { bool checkbox = false; foreach (object attr in attrs) { if (attr is CheckboxAttribute) { checkbox = true; } } if (checkbox) { element = new CheckboxElement(caption, (bool)GetValue(mi, o)); } else { element = new BooleanElement(caption, (bool)GetValue(mi, o)); } } else if (mType == typeof(DateTime)) { var dateTime = (DateTime)GetValue(mi, o); bool asDate = false, asTime = false; foreach (object attr in attrs) { if (attr is DateAttribute) { asDate = true; } else if (attr is TimeAttribute) { asTime = true; } } if (asDate) { element = new DateElement(caption, dateTime); } else if (asTime) { element = new TimeElement(caption, dateTime); } else { element = new DateTimeElement(caption, dateTime); } } else if (mType.IsEnum) { var csection = new Section(); ulong evalue = Convert.ToUInt64(GetValue(mi, o), null); int idx = 0; int selected = 0; foreach (var fi in mType.GetFields(BindingFlags.Public | BindingFlags.Static)) { ulong v = Convert.ToUInt64(GetValue(fi, null)); if (v == evalue) { selected = idx; } CaptionAttribute ca = Attribute.GetCustomAttribute(fi, typeof(CaptionAttribute)) as CaptionAttribute; var cap = ca != null ? ca.Caption : MakeCaption(fi.Name); if (localize) { NSBundle.MainBundle.LocalizedString(cap, cap); } csection.Add(new RadioElement(cap)); idx++; } element = new RootElement(caption, new RadioGroup(null, selected)) { csection }; } else if (mType == typeof(UIImage)) { element = new ImageElement((UIImage)GetValue(mi, o)); } else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(mType)) { var csection = new Section(); int count = 0; if (last_radio_index == null) { throw new Exception("IEnumerable found, but no previous int found"); } foreach (var e in (IEnumerable)GetValue(mi, o)) { csection.Add(new RadioElement(e.ToString())); count++; } int selected = (int)GetValue(last_radio_index, o); if (selected >= count || selected < 0) { selected = 0; } element = new RootElement(caption, new MemberRadioGroup(null, selected, last_radio_index)) { csection }; last_radio_index = null; } else if (typeof(int) == mType) { foreach (object attr in attrs) { if (attr is RadioSelectionAttribute) { last_radio_index = mi; break; } } } else { var nested = GetValue(mi, o); if (nested != null) { var newRoot = new RootElement(caption); Populate(callbacks, nested, newRoot); element = newRoot; } } if (element == null) { continue; } section.Add(element); mappings [element] = new MemberAndInstance(mi, o); } root.Add(section); }