Follow the two steps outlined below to create a custom service.
- Create a service interface that will be available from the View Model layer.
- Create a ServiceBase descendant (or a ViewServiceBase descendant) that implements the service interface.
The ServiceBase class is descended from the Behavior<FrameworkElement> class. Please review the following documentation topic to learn which properties and methods the Behavior<T> class provides and how to use them when creating a Behavior descendant: How to: Create a Custom Behavior.
The example below demonstrates how to create a custom service (a ServiceBase descendant) that provides the capability to show a message box with text defined in Xaml.
using System.Windows.Controls;
namespace Example.View {
public partial class MainView : UserControl {
public MainView() {
InitializeComponent();
}
}
}
<UserControl x:Class="Example.View.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModel="clr-namespace:Example.ViewModel"
xmlns:Service="clr-namespace:Example.Service"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignHeight="500" d:DesignWidth="600">
<UserControl.DataContext>
<ViewModel:MainViewModel/>
</UserControl.DataContext>
<dxmvvm:Interaction.Behaviors>
<Service:CustomMessageBoxService Message="Test Message"/>
</dxmvvm:Interaction.Behaviors>
<Grid x:Name="LayoutRoot" Background="White">
<Button HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10"
Content="Show" Command="{Binding ShowMessageCommand}"/>
</Grid>
</UserControl>
using System.Windows;
namespace Example {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
}
}
using DevExpress.Mvvm;
using Example.Service;
namespace Example.ViewModel {
public class MainViewModel : ViewModelBase {
ICustomMessageBoxService CustomMessageBoxService { get { return GetService<ICustomMessageBoxService>(); } }
public DelegateCommand ShowMessageCommand { get; private set; }
void ShowMessage() {
CustomMessageBoxService.Show();
}
public MainViewModel() {
ShowMessageCommand = new DelegateCommand(ShowMessage);
}
}
}
<Window x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:View="clr-namespace:Example.View"
Title="MainWindow" Height="600" Width="800">
<Grid>
<View:MainView/>
</Grid>
</Window>
namespace Example.Service {
public interface ICustomMessageBoxService {
void Show();
}
}
using DevExpress.Mvvm.UI;
using System.Windows;
namespace Example.Service {
public class CustomMessageBoxService : ServiceBase, ICustomMessageBoxService {
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("Message", typeof(string), typeof(CustomMessageBoxService), new PropertyMetadata("Hello"));
public string Message {
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
void ICustomMessageBoxService.Show() {
MessageBox.Show(Message);
}
}
}
<Application x:Class="Example.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
</Application.Resources>
</Application>
Namespace Example.Service
Public Interface ICustomMessageBoxService
Sub Show()
End Interface
End Namespace
Imports DevExpress.Mvvm.UI
Imports System.Windows
Namespace Example.Service
Public Class CustomMessageBoxService
Inherits ServiceBase
Implements ICustomMessageBoxService
Public Shared ReadOnly MessageProperty As DependencyProperty = DependencyProperty.Register("Message", GetType(String), GetType(CustomMessageBoxService), New PropertyMetadata("Hello"))
Public Property Message() As String
Get
Return CStr(GetValue(MessageProperty))
End Get
Set(ByVal value As String)
SetValue(MessageProperty, value)
End Set
End Property
Private Sub ICustomMessageBoxService_Show() Implements ICustomMessageBoxService.Show
MessageBox.Show(Message)
End Sub
End Class
End Namespace
Imports DevExpress.Mvvm
Imports Example.Service
Namespace Example.ViewModel
Public Class MainViewModel
Inherits ViewModelBase
Private ReadOnly Property CustomMessageBoxService() As ICustomMessageBoxService
Get
Return GetService(Of ICustomMessageBoxService)()
End Get
End Property
Private privateShowMessageCommand As DelegateCommand
Public Property ShowMessageCommand() As DelegateCommand
Get
Return privateShowMessageCommand
End Get
Private Set(ByVal value As DelegateCommand)
privateShowMessageCommand = value
End Set
End Property
Private Sub ShowMessage()
CustomMessageBoxService.Show()
End Sub
Public Sub New()
ShowMessageCommand = New DelegateCommand(AddressOf ShowMessage)
End Sub
End Class
End Namespace
Imports System.Windows
Namespace Example
Partial Public Class App
Inherits Application
Public Sub New()
AddHandler Startup, AddressOf OnStartup
#If SILVERLIGHT Then
InitializeComponent()
#End If
End Sub
Private Overloads Sub OnStartup(ByVal sender As Object, ByVal e As StartupEventArgs)
#If SILVERLIGHT Then
Me.RootVisual = New MainPage()
#Else
MainWindow = New MainWindow()
MainWindow.Show()
#End If
End Sub
End Class
End Namespace
Imports System.Windows.Controls
Namespace Example.View
Partial Public Class MainView
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
End Class
End Namespace
Imports System.Windows
Namespace Example
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
End Class
End Namespace