Back to Devexpress

View Injection Manager Concept

wpf-113941-mvvm-framework-services-predefined-set-viewinjectionservice-view-injection-manager-concept.md

latest10.1 KB
Original Source

View Injection Manager Concept

  • Jun 07, 2019
  • 5 minutes to read

The Concept of the View Injection Manager

The ViewInjectionService provides only a core set of methods and properties that allows you to integrate ViewModels (with their Views) to a component. To inject ViewModels and Views in a more convenient way, use the ViewInjectionManager. It allows you to perform injecting and manipulation under ViewModels and Views in any section of the application’s code.

If your application contains several injectable controls, each of them should have a unique identifier specified using the ViewInjectionService’s RegionName property. This property is used by the ViewInjectionManager to recognize controls (regions) that should be populated with ViewModels and Views.

xaml
<UserControl x:Class="DXSample.View.MainView"
    ...
    xmlns:dxwui="http://schemas.devexpress.com/winfx/2008/xaml/windowsui"
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
    xmlns:common="clr-namespace:DXSample.Common">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <dxui:TileBar Padding="10">
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:ViewInjectionService RegionName="{x:Static common:Regions.Navigation}" />
            </dxmvvm:Interaction.Behaviors>
        </dxui:TileBar>
        <dx:LoadingDecorator Grid.Row="1" >
            <dxwui:FlipView>
                <dxmvvm:Interaction.Behaviors>
                    <dxmvvm:ViewInjectionService RegionName="{x:Static common:Regions.Main}"/>
                </dxmvvm:Interaction.Behaviors>
            </dxwui:FlipView>
        </dx:LoadingDecorator>
    </Grid>
</UserControl>

The Regions is a simple static class.

csharp
public static class Regions {
    public static string Main { get { return "MainRegion"; } }
    public static string Navigation { get { return "NavigationRegion"; } }
}
vb
Public NotInheritable Class Regions
        Public Shared ReadOnly Property Main() As String
            Get
                Return "MainRegion"
            End Get
        End Property
        Public Shared ReadOnly Property Navigation() As String
            Get
                Return "NavigationRegion"
            End Get
        End Property
    End Class

The ViewInjectionManager uses its own ViewInjectionManager.Inject methods.

csharp
public static class ViewInjectionManagerExtensions {
    public static void Inject(this IViewInjectionManager service, string regionName, object key, Func<object> viewModelFactory);
    public static void Inject(this IViewInjectionManager service, string regionName, object key, Func<object> viewModelFactory, string viewName);
    public static void Inject(this IViewInjectionManager service, string regionName, object key, Func<object> viewModelFactory, Type viewType);
    ...
}
vb
Public NotInheritable Class ViewInjectionManagerExtensions
    Private Sub New()
    End Sub
     _
    Public Shared Sub Inject(service As IViewInjectionManager, regionName As String, key As Object, viewModelFactory As Func(Of Object))
    End Sub
     _
    Public Shared Sub Inject(service As IViewInjectionManager, regionName As String, key As Object, viewModelFactory As Func(Of Object), viewName As String)
    End Sub
     _
    Public Shared Sub Inject(service As IViewInjectionManager, regionName As String, key As Object, viewModelFactory As Func(Of Object), viewType As Type)
    End Sub
    ...
End Class
  • The first method overload injects a ViewModel ( viewModelFactory ) marked with a unique identifier ( key ) to the specific region ( regionName ) and uses the view from the control’s ItemTemplate or ContentTemplate (it depends on the control they are populated from.).
  • The other two method overloads inject a ViewModel ( viewModelFactory ) marked with a unique identifier ( key ) to the specific region ( regionName ) and use the View ( viewName or viewType argument) created using the ViewLocator.

Below is a list of method parameters.

  • The property regionName specifies the region name (identifier).
  • The key property specifies the identifier of the View and its ViewModel
  • The viewModelFactory property specifies the ViewModel factory that is invoked when its region is shown
  • Then, the viewName or viewType properties specify the view that will be created using the ViewLocator.

To navigate between injected ViewModels and Views, use the ViewInjectionManager.Navigate method.

csharp
public class ViewInjectionManager : IViewInjectionManager {
    public virtual void Navigate(string regionName, object key) ;
    ...
}
vb
Public Class ViewInjectionManager
    Implements IViewInjectionManager
    Public Overridable Sub Navigate(regionName As String, key As Object)
    End Sub
    ...
End Class

To control when the ViewModel (with its View) becomes active, inactive or closed, the ViewInjectionManager provides the following methods.

csharp
public class ViewInjectionManager : IViewInjectionManager {
    public void RegisterNavigatedEventHandler(object viewModel, Action eventHandler);
    public void RegisterNavigatedAwayEventHandler(object viewModel, Action eventHandler);
    public void RegisterViewModelClosingEventHandler(object viewModel, Action<ViewModelClosingEventArgs> eventHandler);
    ...
}
vb
Public Class ViewInjectionManager
    Implements IViewInjectionManager
    Public Sub RegisterNavigatedEventHandler(viewModel As Object, eventHandler As Action)
    End Sub
    Public Sub RegisterNavigatedAwayEventHandler(viewModel As Object, eventHandler As Action)
    End Sub
    Public Sub RegisterViewModelClosingEventHandler(viewModel As Object, eventHandler As Action(Of ViewModelClosingEventArgs))
    End Sub
    ...
End Class

To unregister event handlers, use the ViewInjectionManager.UnregisterNavigatedAwayEventHandler, ViewInjectionManager.UnregisterNavigatedEventHandler, ViewInjectionManager.UnregisterViewModelClosingEventHandler methods respectively.

csharp
public class ViewInjectionManager : IViewInjectionManager {
    ...
    public void UnregisterNavigatedEventHandler(object viewModel, Action eventHandler = null);
    public void UnregisterNavigatedAwayEventHandler(object viewModel, Action eventHandler = null);
    public void UnregisterViewModelClosingEventHandler(object viewModel, Action<ViewModelClosingEventArgs> eventHandler = null);
}
vb
Public Class ViewInjectionManager
    Implements IViewInjectionManager
    ...
    Public Sub UnregisterNavigatedEventHandler(viewModel As Object, Optional eventHandler As Action = Nothing)
    End Sub
    Public Sub UnregisterNavigatedAwayEventHandler(viewModel As Object, Optional eventHandler As Action = Nothing)
    End Sub
    Public Sub UnregisterViewModelClosingEventHandler(viewModel As Object, Optional eventHandler As Action(Of ViewModelClosingEventArgs) = Nothing)
    End Sub
End Class

To manually invoke an event, you can use one of the Raise* methods.

csharp
public class ViewInjectionManager : IViewInjectionManager {
    public void RaiseNavigatedEvent(object viewModel);
    public void RaiseNavigatedAwayEvent(object viewModel);
    public void RaiseViewModelClosingEvent(ViewModelClosingEventArgs e);
    ...
}
vb
Public Class ViewInjectionManager
    Implements IViewInjectionManager
    Public Sub RaiseNavigatedEvent(viewModel As Object)
    End Sub
    Public Sub RaiseNavigatedAwayEvent(viewModel As Object)
    End Sub
    Public Sub RaiseViewModelClosingEvent(e As ViewModelClosingEventArgs)
    End Sub
    ...
End Class

See Also

View Injection Service Concept

Implementing Custom Strategy