[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Common Concepts
 [Collapse]MVVM Framework
  [Expand]ViewModels
  [Expand]Commands
  [Expand]Behaviors
  [Collapse]Services
    Getting Started
    Services in ViewModelBase descendants
    Services in POCO objects
    Services in custom ViewModels
    View creation mechanisms
   [Expand]Predefined Set
    How to create a Custom Service
  [Expand]DXBinding
  [Expand]MIF
   Converters
   ViewLocator
   Messenger
   Data Annotation Attributes
   LayoutTreeHelper
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Services in custom ViewModels

This documentation topic describes how you can implement support of the Service mechanism in a custom View Model (not derived from the ViewModelBase class and not a POCO ViewModel).

If your View Model is not a POCO View Model and it is not derived from the ViewModelBase class, you can still use the Service mechanism. To implement Service support, implement the ISupportServices interface in your View Model.

The ISupportServices interface is shown below.

To implement the ISupportServices interface, use the following construction.

The ServiceContainer class implements the IServiceContainer interface that provides methods for registering and accessing services.

The IServiceContainer interface is shown below.

When Services (derived from the ServiceBase class) are registered in a View, they analyze their DataContext. If the DataContext is set to an ISupportServices object, a service registers itself within this object. This is accomplished by calling the IServiceContainer.RegisterService method on the ISupportServices.ServiceContainer object. Thus, the service becomes available for use within the ISupportServices object via the IServiceContainer.GetService<T> method.

The following code defines a command within a View Model that displays a message box via a dedicated Message Box service. The service is accessed via the GetService<T> method invoked on the View Model's ServiceContainer protected property. The GetService method retrieves an actual service from a View that implements the specified IMessageBoxService interface.

The following documentation topic contains an example that demonstrates how to support the ISupportService interface in a custom View Model and use services: DXMessageBoxService.

Is this topic helpful?​​​​​​​