Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to create a custom Service (ServiceBase's descendant)

$
0
0

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.

[C#]
publicinterfaceICustomService{voidServiceMethod();}
We will also use this interface in the future to get corresponding service in the ViewModel.
 
2. Then, create a ServiceBase class and the ICustomService interface descendant - the CustomService class:
[C#]
classCustomService:ServiceBase,ICustomService{...publicvoidServiceMethod(){...}}
3. Attach the newly created CustomService to the View:
[XAML]
<Window...<dxmvvm:Interaction.Behaviors><local:CustomServiceTarget="{Binding ElementName=tb}"/></dxmvvm:Interaction.Behaviors><Grid> ...<TextBlockName="tb"VerticalAlignment="Center"HorizontalAlignment="Center"/> ...</Grid></Window>
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:
[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.


Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>