public void OnDialCodeSelectorClick() { if (!Enabled) { return; } var section = new Section(); foreach (var countryCode in CountryCode.CountryCodes) { // this is to generate spaces between dial code number field and country name to align country name in one column // the first column has different size between 0 and 4 characters (1 char for "+" and 3 for country dial code), according // to width of this first column the code inserts additional spaces to align second column (country names) vertically // in currently used font one caracter (A-Z a-z) equals to 2 spaces in its width var prefixSpacing = new string(' ', (Math.Max(3 - countryCode.CountryDialCode.ToString().Length, 0)) * 2); var text = countryCode.CountryDialCode != 0 ? $"{prefixSpacing}+{countryCode.CountryDialCode} {countryCode.CountryName}" : $" {countryCode.CountryName}"; var item = new RadioElementWithId <CountryCode>(countryCode, text, null, false, 15); item.Tapped += () => { SelectedCountryCode = countryCode; if (OnDialCodeChanged != null) { OnDialCodeChanged(_selectedCountryCode); } if (NotifyChanges != null) { NotifyChanges(null, new PhoneNumberChangedEventArgs() { Country = _selectedCountryCode.CountryISOCode }); } }; section.Add(item); } var rootElement = new RootElement(Localize.GetValue("DialCodeSelectorTitle"), new RadioGroup(CountryCode.GetCountryCodeIndexByCountryISOCode(SelectedCountryCode.CountryISOCode))); rootElement.Add(section); var dialCodeSelectorViewController = new TaxiHailDialogViewController(rootElement, true, false, 34); _navigationController.NavigationBar.Hidden = false; _navigationController.NavigationItem.BackBarButtonItem = new UIBarButtonItem(Localize.GetValue("BackButton"), UIBarButtonItemStyle.Bordered, null, null); _navigationController.PushViewController(dialCodeSelectorViewController, true); }
public void Configure <T>(string title, Func <ListItem <T>[]> getValues, Func <Nullable <T> > selectedId, Action <ListItem <T> > onItemSelected, bool allowOther = false) where T : struct { Button.TouchUpInside += (sender, e) => { var values = getValues(); if (values == null) { return; } var selected = -1; var section = new Section(); foreach (var v in values) { // Keep a reference to value in order for callbacks to work correctly var value = v; var display = value.Display; if (!value.Id.HasValue) { display = Localize.GetValue("NoPreference"); } bool showBottomBar = values.Last() != value; if (allowOther) { showBottomBar = true; //other will be last } var item = new RadioElementWithId <T>(value.Id, display, value.Image, showBottomBar); item.Tapped += () => { onItemSelected(value); var controller = this.FindViewController(); if (controller != null) { controller.NavigationController.PopViewController(true); } }; section.Add(item); if (selectedId().Equals(value.Id)) { selected = Array.IndexOf(values, value); } } _rootElement = new RootElement(title, new RadioGroup(selected)); _rootElement.Add(section); var currentController = this.FindViewController(); if (currentController == null) { return; } currentController.View.EndEditing(true); currentController.NavigationController.NavigationBar.Hidden = false; currentController.NavigationItem.BackBarButtonItem = new UIBarButtonItem(Localize.GetValue("BackButton"), UIBarButtonItemStyle.Plain, null, null); var newDvc = new TaxiHailDialogViewController(_rootElement, true, false); if (allowOther) { var other = new OtherElement(); other.KeyboardType = UIKeyboardType.NumberPad; section.Add(other); newDvc.NavigationItem.RightBarButtonItem = new UIBarButtonItem("Done", UIBarButtonItemStyle.Plain, null); newDvc.NavigationItem.RightBarButtonItem.Clicked += (s, eh) => { try { var value = (T)Convert.ChangeType(other.ObjectValue, typeof(T)); onItemSelected(new ListItem <T> { Id = value }); }catch (FormatException) { //display message ? } var controller = this.FindViewController(); if (controller != null) { controller.NavigationController.PopViewController(true); } }; } currentController.NavigationController.PushViewController(newDvc, true); }; }