Skip to main content

EditorAction Class

Definition

Assembly:Avalonia.Controls.RichTextEditor
Package:Avalonia.Controls.RichTextEditor

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

NameDescription
CanExecuteGets whether the action can currently execute.
ExecuteExecutes the action synchronously.
ExecuteAsyncExecutes the action asynchronously.
GetStateGets 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. For toggle actions, this returns a boolean. For property actions, this returns the current property value.

Properties

NameDescription
DisplayNameGets the display name for UI purposes.
GestureGets the optional keyboard gesture for this action.
IdGets 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

When set, this gesture is displayed in tooltips and can be automatically registered as a keyboard shortcut.

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.RichBold, Format.RichItalic, etc.
  • Paragraph.AlignLeft, Paragraph.BulletList, etc.