Not the solution mentioned in the following question.
Dependency Injection in .NET Core 3.0 for WPF
Instead, by adding <EnableDefaultApplicationDefinition>false</EnableDefaultApplicationDefinition>
to the csproj file to prevent App.xaml from automatically generating the Main method, thereby achieving a similar effect to the dependency injection of ASP.NET core.
I have seen a piece of very beautiful code that somehow get an instance of App through IServiceProvider to call Application.Run(), but I can’t remember it now, and I can’t remember the source of the original code. Can anyone help? Thank you very much.
My current code is as follows.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
class Program
{
[STAThread]
public static void Main()
{
Host.CreateDefaultBuilder()
.ConfigureServices(ConfigureServices)
.Build()
.Run();
}
private static void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<App>();
services.AddSingleton<MainWindow>();
}
}
I need to get an instance of the App class to call Application.Run().