ViewInjectionService
- 4 minutes to read
The ViewInjectionService is an IViewInjectionService implementation that allows you to integrate any ViewModel (with its View) to controls.
Note
You are viewing ViewInjectionService documentation. If you’re starting a new project, we strongly recommend that you use Module Injection Framework (MIF).
Assume that you have several views with view models (CustomersView/CustomersViewModel, ProductsView/ ProductsViewModel and SalesView/SalesViewModel) that have to be shown in an items control.
To accomplish this task, place the items control (suppose, it’s a FlipView control) on the MainView and add the ViewInjectionService to the control’s dxmvvm:Interaction.Behaviors collection as usual.
<UserControl x:Class="DXSample.View.MainView"
...
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
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"
xmlns:v="clr-namespace:DXSample.View">
<Grid>
<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>
Inject views with view models using the ViewInjectionManager’s Inject method (To learn more about concepts of ViewInjectionService and ViewInjectionManager, refer to the View Injection Service Concept and View Injection Manager Concept topics respectively). For example, perform injections in the Startup event handler.
public partial class App : Application {
private void Application_Startup(object sender, StartupEventArgs e) {
InitModules();
...
}
private void InitModules() {
ViewInjectionManager.Default.Inject(
Regions.Main,
ModuleType.Customers,
() => CustomersViewModel.Create(),
typeof(CustomersView)
);
ViewInjectionManager.Default.Inject(
Regions.Main,
ModuleType.Sales,
() => SalesViewModel.Create(),
typeof(SalesView)
);
ViewInjectionManager.Default.Inject(
Regions.Main,
ModuleType.Products,
() => ProductsViewModel.Create(),
typeof(ProductsView)
);
ViewInjectionManager.Default.Navigate(Regions.Main, ModuleType.Customers);
}
}
The Regions class is a static class with static string properties.
public static class Regions {
public static string Main { get { return "MainRegion"; } }
public static string Navigation { get { return "NavigationRegion"; } }
}
ModuleType is an enumeration:
The FlipView is populated. Add a navigation view to our MainView.
<UserControl x:Class="DXSample.View.MainView"
…
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
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"
xmlns:v="clr-namespace:DXSample.View">
<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>
Inject items to the TileBar
public partial class App : Application {
private void Application_Startup(object sender, StartupEventArgs e) {
InitModules();
…
}
private void InitModules() {
ViewInjectionManager.Default.Inject(
Regions.Navigation,
ModuleType.Customers,
() => NavigationItemViewModel.Create(…),
typeof(NavigationItemView)
);
ViewInjectionManager.Default.Inject(
Regions.Navigation,
ModuleType.Sales,
() => NavigationItemViewModel.Create(…),
typeof(NavigationItemView)
);
ViewInjectionManager.Default.Inject(
Regions.Navigation,
ModuleType.Products,
() => NavigationItemViewModel.Create(…),
typeof(NavigationItemView)
);
…
ViewInjectionManager.Default.Navigate(Regions.Navigation, ModuleType.Customers);
…
}
}
The example application is almost ready. Synchronize the TileBar and FlipView, so they show the same element. To do this, use the RegisterNavigatedEventHandler.
public class CustomersViewModel {
…
protected CustomersViewModel() {
…
ViewInjectionManager.Default.RegisterNavigatedEventHandler(this, () => {
ViewInjectionManager.Default.Navigate(Regions.Navigation, ModuleType.Customers);
});
}
…
}
public class NavigationItemViewModel {
…
protected NavigationItemViewModel() {
ViewInjectionManager.Default.RegisterNavigatedEventHandler(this, () => {
ViewInjectionManager.Default.Navigate(Regions.Main, ModuleType);
});
}
…
}
By default, ViewInjectionService supports a fixed number of standard and DevExpress controls. However if necessary, you can extend it to work with your own controls. To learn how to do it, refer to Implementing Custom Strategy.