Back to Devexpress

ViewInjectionService

wpf-113750-mvvm-framework-services-predefined-set-viewinjectionservice.md

latest11.0 KB
Original Source

ViewInjectionService

  • Sep 25, 2023
  • 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.

xaml
<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.

csharp
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);
    }
}
vb
Partial Public Class App
    Inherits Application

    Private Sub Application_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
        InitModules()
        ...
    End Sub
    Private Sub InitModules()
        ViewInjectionManager.Default.Inject(
            Regions.Main, 
            ModuleType.Customers, 
            Function() CustomersViewModel.Create(), 
            GetType(CustomersView)
        )
        ViewInjectionManager.Default.Inject(
            Regions.Main, 
            ModuleType.Sales, 
            Function() SalesViewModel.Create(), 
            GetType(SalesView)
        )
        ViewInjectionManager.Default.Inject(
            Regions.Main, 
            ModuleType.Products, 
            Function() ProductsViewModel.Create(), 
            GetType(ProductsView)
        )
        ViewInjectionManager.Default.Navigate(Regions.Main, ModuleType.Customers)
    End Sub
End Class

The Regions class is a static class with static string properties.

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

ModuleType is an enumeration:

csharp
public enum ModuleType { 
    Customers, 
    Sales, 
    Products 
}
vb
Public Enum ModuleType
    Customers
    Sales
    Products
End Enum

The FlipView is populated. Add a navigation view to our MainView.

xaml
<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

csharp
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);
        …                       
    }
}
vb
Partial Public Class App
    Inherits Application
    Private Sub Application_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
        InitModules()
        ...
    End Sub
    Private Sub InitModules()
        ViewInjectionManager.Default.Inject(
            Regions.Navigation, 
            ModuleType.Customers, 
            Function() NavigationItemViewModel.Create(
                "Customers", 
                New BitmapImage(
                    New Uri("../Images/Customers.png",UriKind.RelativeOrAbsolute)
                ), 
                ModuleType.Customers
            ), 
            GetType(NavigationItemView)
        )
        ViewInjectionManager.Default.Inject(
            Regions.Navigation, 
            ModuleType.Sales, 
            Function() NavigationItemViewModel.Create(
                "Sales", 
                New BitmapImage(
                    New Uri("../Images/Sales.png", UriKind.RelativeOrAbsolute)
                ), 
                ModuleType.Sales
            ), 
            GetType(NavigationItemView)
        )
        ViewInjectionManager.Default.Inject(
            Regions.Navigation, 
            ModuleType.Products, 
            Function() NavigationItemViewModel.Create(
                "Products", 
                New BitmapImage(
                    New Uri("../Images/Products.png", UriKind.RelativeOrAbsolute)
                ), 
                ModuleType.Products
            ), 
            GetType(NavigationItemView)
        )
        ...
        ViewInjectionManager.Default.Navigate(Regions.Navigation, ModuleType.Customers)
    End Sub
End Class

The example application is almost ready. Synchronize the TileBar and FlipView , so they show the same element. To do this, use the RegisterNavigatedEventHandler.

csharp
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);
        });
    }
    …
}
vb
Public Class CustomersViewModel
    ...
    Protected Sub New()
        Content = "A representation of CustomersViewModel."
        ViewInjectionManager.Default.RegisterNavigatedEventHandler(Me, Sub() ViewInjectionManager.Default.Navigate(Regions.Navigation, ModuleType.Customers))
    End Sub
    ...
End Class
...
Public Class NavigationItemViewModel
    ...
    Protected Sub New()
        ViewInjectionManager.Default.RegisterNavigatedEventHandler(Me, Sub() ViewInjectionManager.Default.Navigate(Regions.Main, ModuleType))
    End Sub
    ...
End Class

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.

See Also

View Injection Service Concept

View Injection Manager Concept

Implementing Custom Strategy