When starting a project from scratch, there is always some infrastructure that we must build before we can actually get to coding. And since it’s not every day we have to set up a project from scratch, it can be tricky to remember how to accomplish some tasks. In my case, I always have a hard time remembering how to set up dependency injection for WPF projects with Prism. The code inside this article is supposed to be a very simple boilerplate with the fewest possible amount of coding, in order to facilitate the creation of new projects.
There are many articles out there, such as the official ones, for example, that provide us with many details and considerations that we should keep in mind when making decisions for our software. But in some cases, such as when creating a Proof of Concept (PoC), we just want to get stuff working the fastest as possible.
Setting up the Dependency Injection container
Let’s cover the essential parts of our code, which can be found on GitHub. First, we have the Bootstrapper.cs, which creates our Shell. In our scenario, the Shell is simply our Main Window.
internal class Bootstrapper : PrismBootstrapper
{
protected override DependencyObject CreateShell()
{
var mainView = new MainView();
Application.Current.MainWindow = mainView;
mainView.Show();
return mainView;
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
In the App.xaml file, we simply remove the StartupUri property from it. since that’s now defined by our bootstrapper.
<Application x:Class="UltraMegaSimpleDIWithPrism.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
</Application.Resources>
</Application>
Then in the App.xaml.cs class, we override the OnStartup method in order to run our Bootstrapper. After that, our dependency injection container is ready to do its job.
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
}
Connecting Views to their View Models
After having our DI container up and running, let’s link our Views with their respective View Models in the easiest possible way. Below we have our MainView.xaml file. In order for it to find its soulmate, the MainViewModel.cs class, we can use Prism’s auto-wiring feature. If you are curious about how Prisms wires them together, you should check this article.
<Window x:Class="UltraMegaSimpleDIWithPrism.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UltraMegaSimpleDIWithPrism"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBlock Text="{Binding SampleText}"/>
</Grid>
</Window>