private static void CommandOnKeyPressPropertyChanged(BindableObject bindable, object oldValue, object newValue) { if (bindable is Button || bindable is ExtendedLabel) { if (!string.IsNullOrEmpty(KeyPress.GetCommandText(bindable))) { throw new InvalidOperationException("Can't simultaneously set KeyPress.CommandOnKeyPress and KeyPress.CommandText attached properties"); } if (((bool)oldValue == true) && ((bool)newValue == false)) { DeleteControl(bindable); } else if ((bool)newValue) { ReRegisterControl(bindable); bindable.PropertyChanged += (s, e) => { if (e.PropertyName == "Text") { ReRegisterControl(bindable); } }; } } }
private static void ReRegisterControl(BindableObject bindable) { CleanupControls(); var texts = GetCommandKeysFromText(bindable); if (texts != null) { ReAddControl(bindable, texts); var label = bindable as ExtendedLabel; if (label != null && KeyPress.GetHighlightKeys(label) && KeyPress.HighlightStyle != null && !string.IsNullOrEmpty(KeyPress.GetCommandText(label))) { var text = KeyPress.GetCommandText(label); var t0 = text.IndexOf('[') != 0 ? text.Substring(0, text.IndexOf('[')) : string.Empty; var t1 = text.Substring(text.IndexOf('['), text.IndexOf(']') - t0.Length + 1); var t2 = text.IndexOf(']') < text.Length ? text.Substring(text.IndexOf(']') + 1, text.Length - t1.Length - t0.Length) : string.Empty; var ft = new FormattedString(); if (!string.IsNullOrEmpty(t0)) { ft.Spans.Add(new Span() { Text = t0 }); } ft.Spans.Add(new Span() { Text = t1, Style = HighlightStyle }); if (!string.IsNullOrEmpty(t2)) { ft.Spans.Add(new Span() { Text = t2 }); } label.FormattedText = ft; } } else { DeleteControl(bindable); } }
private static string[] GetCommandKeysFromText(BindableObject view) { if (!(view is Button || view is ExtendedLabel)) { return(null); } var text = KeyPress.GetCommandText(view); if (string.IsNullOrEmpty(text)) { text = view is Button ? (view as Button).Text : (view as ExtendedLabel).Text; } if (!string.IsNullOrEmpty(text)) { var firstPos = text.IndexOf('['); var secondPos = text.IndexOf(']'); if (secondPos - firstPos - 1 <= 0) { return(null); } text = text.Trim().ToLower().Substring(firstPos + 1, secondPos - firstPos - 1); if (text.Contains(",")) { var texts = text.Split(','); for (int i = 0; i < texts.Length; i++) { texts[i] = texts[i].Trim(); } return(texts); } return(new string[] { text }); } return(null); }