Back to Devexpress

View Injection Service Concept

wpf-113942-mvvm-framework-services-predefined-set-viewinjectionservice-view-injection-service-concept.md

latest5.5 KB
Original Source

View Injection Service Concept

  • Jun 07, 2019
  • 3 minutes to read

The Concept of the View Injection Service

The main benefit of the ViewInjectionService is that it provides a common mechanism to control ViewModels and their interaction.

To start using this service, add it to the control’s dxmvvm:Interaction.Behaviors.

xaml
<UserControl x:Class="DXSample.View.MainView"
    ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
    xmlns:common="clr-namespace:DXSample.Common">
    <Grid>
        <TabControl>
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:ViewInjectionService/>
            </dxmvvm:Interaction.Behaviors>
        </TabControl>
    </Grid>
</UserControl>

Use any of the following approaches to access the defined ViewInjectionservice from your ViewModel.

To integrate a ViewModel (with its View) to the TabControl , use the ViewInjectionServiceExtensions.Inject method. This method has three overloads with different parameters.

csharp
public static class ViewInjectionServiceExtensions {
    public static void Inject(this IViewInjectionService service, object key, object viewModel);
    public static void Inject(this IViewInjectionService service, object key, object viewModel, string viewName);
    public static void Inject(this IViewInjectionService service, object key, object viewModel, Type viewType);
    ...
}
vb
Public NotInheritable Class ViewInjectionServiceExtensions
     _
    Public Shared Sub Inject(service As IViewInjectionService, key As Object, viewModel As Object)
    End Sub
     _
    Public Shared Sub Inject(service As IViewInjectionService, key As Object, viewModel As Object, viewName As String)
    End Sub
     _
    Public Shared Sub Inject(service As IViewInjectionService, key As Object, viewModel As Object, viewType As Type)
    End Sub
    ...
End Class
  • The first method overload injects the ViewModel passed to a control using viewModel parameter. The View is defined in the control’s ItemTemplate or ContentTemplate.
  • The other two method overloads use the ViewLocator to create a View by using viewName or viewType arguments and pass the specified ViewModel to the created View.

Tip

A more advanced way to integrate ViewModels and Views into a control is to use ViewInjectionManager. This concept is described in the View Injection Manager Concept topic.

Below is a list of other general ViewInjectionService properties and members.

csharp
public sealed class ViewInjectionService : ServiceBase, IViewInjectionService {
    public string RegionName { ... }
    public IEnumerable<object> ViewModels { ... }
    public object SelectedViewModel { ... }
    public ICommand SelectedViewModelChangedCommand { ... }
    public ICommand ViewModelClosingCommand { ... }
    ...
}
vb
Public NotInheritable Class ViewInjectionService
    Inherits ServiceBase
    Inherits ViewInjectionService
    Public Property RegionName() As String
        ...
    End Property
    Public Property ViewModels() As IEnumerable(Of Object)
        ...
    End Property
    Public Property SelectedViewModel() As Object
        ...
    End Property
    Public Property SelectedViewModelChangedCommand() As ICommand
        ...
    End Property
    Public Property ViewModelClosingCommand() As ICommand
        ...
    End Property
End Class

See Also

View Injection Manager Concept

Implementing Custom Strategy