wpf-113941-mvvm-framework-services-predefined-set-viewinjectionservice-view-injection-manager-concept.md
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.
<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.
public static class Regions {
public static string Main { get { return "MainRegion"; } }
public static string Navigation { get { return "NavigationRegion"; } }
}
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.
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);
...
}
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
Below is a list of method parameters.
To navigate between injected ViewModels and Views, use the ViewInjectionManager.Navigate method.
public class ViewInjectionManager : IViewInjectionManager {
public virtual void Navigate(string regionName, object key) ;
...
}
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.
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);
...
}
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.
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);
}
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.
public class ViewInjectionManager : IViewInjectionManager {
public void RaiseNavigatedEvent(object viewModel);
public void RaiseNavigatedAwayEvent(object viewModel);
public void RaiseViewModelClosingEvent(ViewModelClosingEventArgs e);
...
}
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