csharpdeeb25a2 | 1 | using Avalonia; |
| 2 | using Avalonia.Controls; |
| 3 | using Avalonia.VisualTree; |
| 4 | |
| 5 | namespace CascadeIDE.Services; |
| 6 | |
| 7 | /// <summary> |
| 8 | /// Установить текст в контрол, поддерживающий ввод (TextBox и т.п.). Вызывать из UI-потока. Для ide_set_control_text. |
| 9 | /// </summary> |
| 10 | public static class UiControlSetText |
| 11 | { |
| 12 | public static string SetText(Visual root, string controlName, string text) |
| 13 | { |
| 14 | if (string.IsNullOrWhiteSpace(controlName)) |
| 15 | return "Missing control name."; |
| 16 | |
| 17 | var control = root is Window mw |
| 18 | ? UiControlAppearance.FindControlByNameAcrossAllWindows(mw, controlName.Trim()) |
| 19 | : UiControlAppearance.FindControlByName(root, controlName.Trim()); |
| 20 | if (control is null) |
| 21 | return $"Control not found: {controlName}."; |
| 22 | |
| 23 | if (control is TextBox tbx) |
| 24 | { |
| 25 | tbx.Text = text ?? ""; |
| 26 | return "OK"; |
| 27 | } |
| 28 | |
| 29 | var contentProp = control.GetType().GetProperty("Content"); |
| 30 | if (contentProp is not null && contentProp.CanWrite) |
| 31 | { |
| 32 | contentProp.SetValue(control, text ?? ""); |
| 33 | return "OK"; |
| 34 | } |
| 35 | |
| 36 | return $"Control does not support text input (not TextBox and no writable Content): {control.GetType().Name}."; |
| 37 | } |
| 38 | } |
| 39 | |
View only · write via MCP/CIDE