PdfViewer.AddService(Type, ServiceCreatorCallback, Boolean) Method
Adds the specified service to the service container.
Namespace: DevExpress.XtraPdfViewer
Assembly: DevExpress.XtraPdfViewer.v19.1.dll
Declaration
Parameters
Name | Type | Description |
---|---|---|
serviceType | Type | The type of service to add. |
callback | ServiceCreatorCallback | A callback object that is used to create the service. This allows a service to be declared as available, but delays the creation of the object until the service is requested. |
promote | Boolean | true, to promote this request to any parent service containers; otherwise, false. |
Remarks
The PdfViewer implements the IServiceProvider and IServiceContainer interfaces. In addition to providing services, it also provides a mechanism for adding and removing services. To obtain a service, call the PdfViewer.GetService<T> method.
When a service is added, it can be added with instructions to promote it. When a service is promoted, it is added to any parent service container, on up, until the top of the service container tree is reached.
Example
This example shows how to modify the functionality of an existing PdfViewer command.
The PdfViewer exposes the IPdfViewerCommandFactoryService interface that enables you to substitute the default command with your own custom command.
To accomplish this task:
- Create a custom command class (e.g., a CustomNextPageCommand class), inherited from the command that you wish to replace (e.g., PdfNextPageCommand).
- Override the required method of the command. The main functionality and command specifics are located in the Execute method.
- Create a class (e.g., CustomPdfViewerCommandFactoryService) implementing the IPdfViewerCommandFactoryService. You should override the IPdfViewerCommandFactoryService.CreateCommand method to create an instance of a custom command class if an identifier of a certain command is passed as a parameter. So, instead of the default command, a custom command will be used by the PdfViewer.
- Use the created class to substitute for the default PdfViewer’s service.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-replace-standard-pdf-viewer-control-command-with-your-own-custom-command-t328206
- Form1.cs
- CustomPdfViewerCommandFactoryService.cs
- Program.cs
- CustomNextPageCommand.cs
- Program.vb
- CustomNextPageCommand.vb
- Form1.vb
- CustomPdfViewerCommandFactoryService.vb
using DevExpress.XtraPdfViewer;
using System;
using System.Windows.Forms;
namespace ViewerCustomCommand {
public partial class Form1 : Form {
void ReplacePdfViewerCommandFactoryService(PdfViewer control) {
IPdfViewerCommandFactoryService service = control.GetService<IPdfViewerCommandFactoryService>();
if (service == null)
return;
control.RemoveService(typeof(IPdfViewerCommandFactoryService));
control.AddService(typeof(IPdfViewerCommandFactoryService), new CustomPdfViewerCommandFactoryService(control,service));
}
public Form1() {
InitializeComponent();
pdfViewer1.LoadDocument("..\\..\\Demo.pdf");
ReplacePdfViewerCommandFactoryService(pdfViewer1);
}
}
}