[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
 [Expand]What's Installed
 [Expand].NET Core 3 Support
 [Expand]Common Concepts
 [Collapse]MVVM Framework
  [Expand]View Models
    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
   Data Annotation Attributes
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

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?​​​​​​​