public static void FindAndExecuteCommand(string key) { for (int i = 0; i < attachedControls.Count; i++) { for (int k = 0; k < attachedControls[i].keys.Length; k++) { if (attachedControls[i].keys[k] == key) { var visibleEnabled = (attachedControls[i].bindable as VisualElement).IsEnabled && (attachedControls[i].bindable as VisualElement).IsVisible && !attachedControls[i].AnyParentInvisible; if (visibleEnabled && modalParents.Count > 0) { visibleEnabled = attachedControls[i].IsParent(modalParents.Peek()); } if (visibleEnabled) { var clickControl = KeyPress.GetClickControl(attachedControls[i].bindable); if (clickControl != null) { if (clickControl is ExtendedLabel) { (clickControl as ExtendedLabel).DoClick(); } } else { if (attachedControls[i].bindable is Button) { (attachedControls[i].bindable as Button)?.Command?.Execute((attachedControls[i].bindable as Button).CommandParameter); } else if (attachedControls[i].bindable is ExtendedLabel) { (attachedControls[i].bindable as ExtendedLabel)?.Command?.Execute((attachedControls[i].bindable as ExtendedLabel).CommandParameter); } } } } } } }
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); }
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); } }