Services is a special mechanism that provides the capability to perform View-related actions at the View Model level. This example demonstrates how to create a custom Service (ServiceBase's descendant). For this, it is necessary to perform the following steps:
1. Create interface with required method definitions.
We will also use this interface in the future to get corresponding service in the ViewModel.[C#]publicinterfaceICustomService{voidServiceMethod();}
2. Then, create a ServiceBase class and the ICustomService interface descendant - the CustomService class:
3. Attach the newly created CustomService to the View:[C#]classCustomService:ServiceBase,ICustomService{...publicvoidServiceMethod(){...}}
4. To get access to the CustomService at the ViewModel level, it's necessary to inherit the ViewModel from our ViewModelBase class and use the ViewModelBase's GetService method in the following manner:[XAML]<Window...<dxmvvm:Interaction.Behaviors><local:CustomServiceTarget="{Binding ElementName=tb}"/></dxmvvm:Interaction.Behaviors><Grid> ...<TextBlockName="tb"VerticalAlignment="Center"HorizontalAlignment="Center"/> ...</Grid></Window>
[C#]classViewModel:ViewModelBase{publicICustomServiceService{get{returnGetService<ICustomService>();}}publicICommandCommand{get;privateset;}publicViewModel(){Command=newDelegateCommand(CommandAction);}privatevoidCommandAction(){Service.ServiceMethod();}}
You can find more information about Services in our documentation. This page contains links to Code Examples for each Service.