Skip to content

3. Getting started πŸš€

Artyom M edited this page Jun 29, 2022 · 4 revisions

Minimal setup

To start using Microsoft.Extensions.Hosting.Wpf you need to add following changes.

Add IApplicationInitializeComponent to App.cs

This will help Microsoft.Extensions.Hosting.Wpf to find InitializeComponent that WPF in generating.

public partial class App : Application, IApplicationInitializeComponent
{
}

Create Program.cs

We need to add new Main entry point for our WPF application.

public class Program
{
	/// <summary>
	/// The main entry point for the application.
	/// </summary>
	public static void Main(string[] args)
	{
		using IHost host = CreateHostBuilder(args)
			.Build();
		host.Run();
	}

	private static IHostBuilder CreateHostBuilder(string[] args)
	{
		return Host.CreateDefaultBuilder(args)
			.ConfigureServices(ConfigureServices);
	}

	private static void ConfigureServices(HostBuilderContext hostContext, IServiceCollection services)
	{
		services.AddWpf<App>();
	}
}

Modify WPF project .csproj

Now we need to change our csproj in order to use that new entry point.

<StartupObject>[Namespace].Program</StartupObject>