Skip to main content

RichTextEditor issues

info

This control is available as part of Avalonia Pro or higher.

For reference information on this control, see the RichTextEditor page.

A viewer shows nothing​

FlowDocumentScrollViewer.Document and FlowDocumentPageViewer.Document start out null, so a viewer with no document assigned renders an empty surface rather than reporting a problem.

viewer.Document = new FlowDocument
{
Blocks = { new Paragraph { Inlines = { new RichRun("Hello") } } }
};

RichTextEditor.Document behaves differently: the editor creates an empty FlowDocument for itself, so it is never null unless you assign null over it.

Undo does nothing​

The editor creates an UndoManager for you when a document is attached, so there is nothing to install. RichTextEditor.UndoManager is read-only and reports the one in use.

Two things do switch undo off:

// Recording is disabled
editor.UndoManager!.IsEnabled = false;

// Or the history is too short to hold the edit
editor.UndoLimit = 0;

A document you built yourself and assigned to TextDocument.UndoManager is used as-is; the editor adopts an existing manager rather than replacing it.

Edits do not appear​

Group several operations into one change so the view updates once. Undo treats the group as a single step. BeginChange returns an IDisposable, so the scope ends with the using block.

using (document.TextDocument.BeginChange())
{
// Your edits
}

A document loaded on a background thread throws on first use​

A FlowDocument and its elements bind to the dispatcher of the thread that constructs them, so a document assembled on a pool thread throws InvalidOperationException the first time the UI thread reads a property from it.

FlowDocument.LoadAsync handles this: it parses off the calling thread and then builds the element tree through the UI thread dispatcher.

To stay off the UI thread entirely, work with the model rather than the facade. TextDocument is not thread-bound:

var snapshot = serializer.Deserialize(stream);
var document = TextDocument.FromSnapshot(snapshot);

Text in an exported PDF is unreadable​

A font whose OS/2 fsType forbids embedding cannot be embedded. The export substitutes one. Ask the exporter what it did rather than guessing:

var options = new PdfSerializerOptions
{
Diagnostics = d => Console.WriteLine($"{d.Kind}: {d.Message}")
};

Debugging threading issues​

Enable thread assertions​

Avalonia has built-in thread checking:

// Throws if not on UI thread
Dispatcher.UIThread.VerifyAccess();

Common exceptions​

InvalidOperationException: "The calling thread cannot access this object because a different thread owns it."

  • Cause: A FlowDocument or an element was constructed on one thread and read from another.
  • Fix: Build the facade on the UI thread, or use TextDocument and DocumentSnapshot, which are not thread-bound.

InvalidOperationException from a serializer during editing.

  • Cause: serializing a live document while it is being edited.
  • Fix: Capture a DocumentSnapshot on the UI thread first, then serialize the snapshot from wherever you like.

See also​