EditorAction Class
Definition
Abstract base class for editor actions.
public class EditorAction
Remarks
Provides default implementations for common functionality. Derived classes should override Avalonia.Controls.Documents.Primitives.Actions.EditorAction.Id, Avalonia.Controls.Documents.Primitives.Actions.EditorAction.DisplayName, Avalonia.Controls.Documents.Primitives.Actions.EditorAction.CanExecute(Avalonia.Controls.Documents.Primitives.ITextEditorHost), and Avalonia.Controls.Documents.Primitives.Actions.EditorAction.Execute(Avalonia.Controls.Documents.Primitives.ITextEditorHost).
Methods
| Name | Description |
|---|---|
| CanExecute | Gets whether the action can currently execute. |
| Execute | Executes the action synchronously. |
| ExecuteAsync | Executes the action asynchronously. |
| GetState | Gets the current state of the action. |
CanExecute Method
Gets whether the action can currently execute.
public bool CanExecute(Avalonia.Controls.Documents.Primitives.ITextEditorHost host)
Parameters
host Avalonia.Controls.Documents.Primitives.ITextEditorHost
The editor host to check against.
Returns
bool
True if the action can execute; otherwise, false.
Remarks
This is called frequently to update UI state (button enabled/disabled). Implementations should be fast and avoid heavy computation.
Execute Method
Executes the action synchronously.
public void Execute(Avalonia.Controls.Documents.Primitives.ITextEditorHost host)
Parameters
host Avalonia.Controls.Documents.Primitives.ITextEditorHost
The editor host to execute on.
Remarks
For actions that require async operations (like file dialogs), use Avalonia.Controls.Documents.Primitives.Actions.IEditorAction.ExecuteAsync(Avalonia.Controls.Documents.Primitives.ITextEditorHost) instead.
ExecuteAsync Method
Executes the action asynchronously.
public System.Threading.Tasks.Task ExecuteAsync(Avalonia.Controls.Documents.Primitives.ITextEditorHost host)
Parameters
host Avalonia.Controls.Documents.Primitives.ITextEditorHost
The editor host to execute on.
Returns
System.Threading.Tasks.Task
A task representing the asynchronous operation.
Remarks
The default implementation calls Avalonia.Controls.Documents.Primitives.Actions.IEditorAction.Execute(Avalonia.Controls.Documents.Primitives.ITextEditorHost) synchronously. Override this for actions that need async operations like file dialogs.
GetState Method
Gets the current state of the action.
public object GetState(Avalonia.Controls.Documents.Primitives.ITextEditorHost host)
Parameters
host Avalonia.Controls.Documents.Primitives.ITextEditorHost
The editor host to query.
Returns
object
The current state, or null if the action has no state. A toggle action returns a boolean, a property action the current value.
Remarks
Read Avalonia.Controls.Documents.Primitives.Actions.IToggleAction.IsChecked(Avalonia.Controls.Documents.Primitives.ITextEditorHost) or Avalonia.Controls.Documents.Primitives.Actions.IPropertyAction.GetValueAsObject(Avalonia.Controls.Documents.Primitives.ITextEditorHost) instead. Both say which family the value came from, which an untyped object cannot.
Properties
| Name | Description |
|---|---|
| DisplayName | Gets the display name for UI purposes. |
| Gesture | Gets the optional keyboard gesture for this action. |
| Id | Gets the unique identifier for this action. |
DisplayName Property
Gets the display name for UI purposes.
public string DisplayName { get; set; }
Remarks
This is used for tooltips, menu items, and accessibility.
Gesture Property
Gets the optional keyboard gesture for this action.
public Avalonia.Input.KeyGesture Gesture { get; set; }
Remarks
The gesture is what tooltips and menu items display. It registers nothing: the shortcut that actually fires is handled by the editor's keyboard component, so an action whose gesture differs from the one the editor handles advertises a shortcut that does nothing.
Id Property
Gets the unique identifier for this action.
public string Id { get; set; }
Remarks
IDs follow a hierarchical naming convention:
- Edit.Undo, Edit.Redo, Edit.Cut, etc.
- Format.Bold, Format.Italic, etc.
- Block.TextAlignment.Left, List.ToggleBulletList, etc. Avalonia.Controls.Documents.Primitives.Actions.EditorActionIds names the identifier of every built-in action.