[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)

Getting Started

A Service is a special Behavior that implements an interface. Although Services are defined in Xaml, the Service interfaces can accessed from the View Model layer.

Assume that you need to show a message box from a View Model. The easiest way to accomplish this task is to use the MessageBox.Show method directly from the View Model. However, this approach breaks the main MVVM rule: the View Model layer should not refer to the View layer. Thus, this way makes it impossible to write unit tests for this View Model, because there is no one who can click the MessageBox button during a unit test. For solving such tasks in MVVM, use the Services mechanism.

Let's discuss how to solve the posed task with Services. We have the following View Model…

… and the following View:

The DevExpress.Xpf.Mvvm library provides the IMessageBoxService interface. Implementation of this interface is contained in the DevExpress.Xpf.Core library – the DXMessageBoxService class. To add this service to our View (DocumentView), add it to the Interaction.Behaviors collection as follows.


Certain services that don't need to be associated with a specific control (like NotificationService, DispatcherService, etc) can be registered at the App.xaml level.

To access the application services from other than a view model, you can use the static Default property of the DevExpress.Mvvm.ServiceContainer class:

Services are automatically injected to View Models, so they are available from there via an interface that is provided by a certain service.

As you may have noticed, our View Model (DocumentViewModel) is inherited from the ViewModelBase class. So, the DocumentViewModel supports the GetService<T> method that returns an interface used to access the DXMessageBoxService.

Thus, follow the steps below to use Services.

  • Set the View's DataContext to your View Model.
  • Define a service in XAML.
  • Get access to the service's interface from your View Model via the GetService<T> method.

The use of Services makes it easy to create unit-tests for your View Models. Let's write a test for the above-mentioned DocumentViewModel (Moq Framework is used).


You can download an example on how to use DXMessageBoxService from the following topic: DXMessageBoxService.

Services become available only when Views to which services are attached are loaded. To obtain services to perform preliminary actions, handle the View's Loaded event (for example, using EventToCommand) and access the required service there. Below is a code snippet that shows how to perform preliminary navigation using the FrameNavigationService.

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