Services in Rich Text Editor for WPF
- 3 minutes to read
To minimize the chance of breaking changes in future versions, we have separated the actual implementation of a feature in code from the implementation of methods used to access this feature. We manage this task via extensive use of interfaces. It was also necessary to find a way to break apart dependencies in the application and achieve looser coupling between its parts. This approach usually results in greater extensibility.
To achieve this, we follow a concept of services and service containers, implemented in .NET Framework.
Service is a class with a known interface, addressable by its type. A service can be obtained from a service provider, instantiated and stored within a service container. The RichEdit Control incorporates classes and interfaces that inherit from base interfaces, contained within the System.ComponentModel namespace of .NET - firstly, the IServiceProvider and IServiceContainer. You can obtain a service by using the generic RichEditControl.GetService<T> method.
When you get a service via the GetService method, you actually use the specified interface to obtain a pointer to an object that implements the methods of this interface. You can save this pointer (and the object it references), and delete the service implementation from the RichEditControl with the help of the RichEditControl.RemoveService method. When it is done, the corresponding GetService method will return null.
At that time, you can install your own service using the RichEditControl.AddService method. Subsequent calls to GetService will return the pointer to an object representing your service. So, the calls to service methods are redirected from the default implementation to a custom one. It is done silently and transparently for the caller.
This mechanism enables you to modify the default behavior of an application that uses these services at any time, without re-designing it.
Tip
Instead of the utilizing the GetService -> RemoveService -> AddService method sequence you can use the generic IRichEditDocumentServer.ReplaceService<T> method (the RichEditControl.ReplaceService<T> method for the RichEditControl).
RichEditControl implements the following services.
Service | Description |
---|---|
IRichEditCommandFactoryService | Enables you to substitute default RichEdit commands or create your own. |
IUriProviderService | Enables you to specify custom locations for exported images and styles. |
IUriStreamService | Used to retrieve data from the URI, specified in some types of document fields. |
IUserListService | Provides a list of identities to fill the Editing Permissions form. |
IUserGroupListService | Provides a list of user groups to fill the Editing Permissions form. |
IAutoCorrectService | Defines a service that performs auto correction. |
ISyntaxHighlightService | Enables you to implement the text highlighting functionality. |
IProgressIndicationService | Allows you to display a progress indicator for time-consuming operations. See the sample project available in the DevExpress Code Central database at https://supportcenter.devexpress.com/ticket/details/e3293/dxrichedit-for-wpf-how-to-implement-progress-indicator-for-slow-richeditcontrol-operations. |
Note
Register the service after the RichEditControl is completely loaded, i.e. in the RichEditControl.Loaded event handler.