TX Text Control .NET for WPF

Control Initialization

Topic

The WPF TextControl is ready to work with a document once WPF has loaded it into the window. Code that runs earlier, for example in the constructor of the window, finds a control that isn't ready yet. The samples and tutorials of TX Text Control therefore run their startup code in the Loaded event of the control, and the problems below come from code that doesn't.

Code in the Constructor Has No Effect or Fails

Code that loads a document, inserts text or reads the version of the control fails or has no effect when it runs in the constructor of the window, while the same code works later, for example when a button is clicked.

When the constructor runs, WPF hasn't loaded the control into the window yet. Many members of the TextControl, such as Selection, are available at run time only, and the tutorials and samples of TX Text Control use them from the Loaded event of the control.

  1. Connect a handler to the Loaded event of the TextControl, in XAML with Loaded="TextControl1_Loaded" or in the constructor after InitializeComponent.
  2. Move the startup code into this handler.

Control Initialization and Document Access explains the lifecycle of the control, the members that exist only at run time and the difference between Loaded and DocumentLoaded, with a complete example.

A File Passed at Start Isn't Loaded

Your application opens a file that is passed on the command line or from another window, but the TextControl stays empty.

The file is loaded before the control is ready. The TX Words samples open such a file only after the Loaded event of the control.

  1. Keep the name of the file in a field of the window.
  2. In the Loaded handler of the TextControl, call Load with it, as Open a Document When the Window Opens shows.

GetLargeIcon or GetSmallIcon Throws an Exception

ResourceProvider.GetLargeIcon or GetSmallIcon throws an exception with this message:

The specified framework element has not been loaded for presentation.

The overloads that take a FrameworkElement need the element to be loaded. GetLargeIcon or GetSmallIcon Throws an Exception describes the fix.

A ServerTextControl Fails

A ServerTextControl, which processes documents without a user interface, throws this message:

The TXTextControl object must have been completely loaded to use this method.

A ServerTextControl isn't loaded into a window: it must be initialized with Create before any other member is used. The TXTextControl Object Must Have Been Completely Loaded lists the fixes.