windowsforms-devexpress-dot-xtrapdfviewer-dot-pdfviewer-dot-getservice-1.md
Gets the specified service.
Namespace : DevExpress.XtraPdfViewer
Assembly : DevExpress.XtraPdfViewer.v25.2.dll
NuGet Package : DevExpress.Win.PdfViewer
public T GetService<T>()
where T : class
Public Function GetService(Of T As Class) As T
| Name |
|---|
| T |
| Type | Description |
|---|---|
| T |
A service object of the specified generic type, or a null reference ( Nothing in Visual Basic) if there is no service object of this type.
|
Use this method to enable your application objects to obtain a service of the PDF Viewer control, in order to employ methods of the service. The control implements the IServiceProvider interface, so you can tell it what type of service you wish to retrieve; and if a service is available, it is offered to the caller object.
There is a set of predefined services you can retrieve. The IServiceProvider and IServiceContainer interface realization allows hiding implementation details, and provides more flexibility regarding future updates of the component.
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:
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);
}
}
}
using DevExpress.Utils;
using DevExpress.XtraPdfViewer;
using DevExpress.XtraPdfViewer.Commands;
namespace ViewerCustomCommand {
public class CustomPdfViewerCommandFactoryService : IPdfViewerCommandFactoryService {
readonly IPdfViewerCommandFactoryService service;
readonly PdfViewer control;
public CustomPdfViewerCommandFactoryService(PdfViewer control,
IPdfViewerCommandFactoryService service) {
Guard.ArgumentNotNull(control, "control");
Guard.ArgumentNotNull(service, "service");
this.control = control;
this.service = service;
}
public PdfViewerCommand CreateCommand(PdfViewerCommandId id) {
if (id == PdfViewerCommandId.NextPage)
return new CustomNextPageCommand(control);
return service.CreateCommand(id);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ViewerCustomCommand {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
using DevExpress.XtraPdfViewer;
using DevExpress.XtraPdfViewer.Commands;
using System.Windows.Forms;
namespace ViewerCustomCommand {
public class CustomNextPageCommand : PdfNextPageCommand {
public CustomNextPageCommand(PdfViewer control)
: base(control) {
}
public override void Execute() {
MessageBox.Show("Custom command executed");
base.Execute();
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Threading.Tasks
Imports System.Windows.Forms
Namespace ViewerCustomCommand
Friend NotInheritable Class Program
Private Sub New()
End Sub
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1())
End Sub
End Class
End Namespace
Imports DevExpress.XtraPdfViewer
Imports DevExpress.XtraPdfViewer.Commands
Imports System.Windows.Forms
Namespace ViewerCustomCommand
Public Class CustomNextPageCommand
Inherits PdfNextPageCommand
Public Sub New(ByVal control As PdfViewer)
MyBase.New(control)
End Sub
Public Overrides Sub Execute()
MessageBox.Show("Custom command executed")
MyBase.Execute()
End Sub
End Class
End Namespace
Imports DevExpress.XtraPdfViewer
Imports System
Imports System.Windows.Forms
Namespace ViewerCustomCommand
Partial Public Class Form1
Inherits Form
Private Sub ReplacePdfViewerCommandFactoryService(ByVal control As PdfViewer)
Dim service As IPdfViewerCommandFactoryService = control.GetService(Of IPdfViewerCommandFactoryService)()
If service Is Nothing Then
Return
End If
control.RemoveService(GetType(IPdfViewerCommandFactoryService))
control.AddService(GetType(IPdfViewerCommandFactoryService), New CustomPdfViewerCommandFactoryService(control,service))
End Sub
Public Sub New()
InitializeComponent()
pdfViewer1.LoadDocument("..\..\Demo.pdf")
ReplacePdfViewerCommandFactoryService(pdfViewer1)
End Sub
End Class
End Namespace
Imports DevExpress.Utils
Imports DevExpress.XtraPdfViewer
Imports DevExpress.XtraPdfViewer.Commands
Namespace ViewerCustomCommand
Public Class CustomPdfViewerCommandFactoryService
Implements IPdfViewerCommandFactoryService
Private ReadOnly service As IPdfViewerCommandFactoryService
Private ReadOnly control As PdfViewer
Public Sub New(ByVal control As PdfViewer, ByVal service As IPdfViewerCommandFactoryService)
Guard.ArgumentNotNull(control, "control")
Guard.ArgumentNotNull(service, "service")
Me.control = control
Me.service = service
End Sub
Public Function CreateCommand(id As PdfViewerCommandId) As PdfViewerCommand Implements IPdfViewerCommandFactoryService.CreateCommand
If id = PdfViewerCommandId.NextPage Then
Return New CustomNextPageCommand(control)
End If
Return service.CreateCommand(id)
End Function
End Class
End Namespace
The following code snippets (auto-collected from DevExpress Examples) contain references to the GetService<T>() method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
winforms-pdf-viewer-replace-standard-command-with-custom-command/CS/ViewerCustomCommand/Form1.cs#L9
void ReplacePdfViewerCommandFactoryService(PdfViewer control) {
IPdfViewerCommandFactoryService service = control.GetService<IPdfViewerCommandFactoryService>();
if (service == null)
how-to-custom-draw-in-pdf-viewer/CS/PDF_Viewer/Form1.cs#L122
var service = pdfViewer.GetService<IPdfViewerCommandFactoryService>();
pdfViewer.RemoveService(typeof(IPdfViewerCommandFactoryService));
winforms-pdf-viewer-replace-standard-command-with-custom-command/VB/ViewerCustomCommand/Form1.vb#L10
Private Sub ReplacePdfViewerCommandFactoryService(ByVal control As PdfViewer)
Dim service As IPdfViewerCommandFactoryService = control.GetService(Of IPdfViewerCommandFactoryService)()
If service Is Nothing Then Return
how-to-custom-draw-in-pdf-viewer/VB/PDF_Viewer/Form1.vb#L123
AddHandler pdfViewer.MouseWheel, AddressOf PdfViewer_MouseWheel
Dim service = pdfViewer.GetService(Of IPdfViewerCommandFactoryService)()
pdfViewer.RemoveService(GetType(IPdfViewerCommandFactoryService))
See Also