第一种方式(基于 Application 类的静态方法):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| public partial class App : Application
{
public IServiceProvider ServiceProvider { get; }
public new static App Current => (App)Application.Current;
public App()
{
ServiceProvider = ConfigureServices();
}
private static IServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
services.AddSingleton<MainWindow>();
services.AddSingleton<MainWindowViewModel>();
services.AddDbContext<DefaultDbContext>();
services.AddSingleton<ILogger>(_ =>
{
return new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File("Log/log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
});
return services.BuildServiceProvider();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
mainWindow.Show();
}
}
|
1
2
3
4
5
6
7
| <Application
x:Class="Wpf_onlyDI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Wpf_onlyDI" Startup="Application_Startup">
<Application.Resources />
</Application>
|
1
2
3
4
5
6
7
8
| public partial class MainWindow : Window
{
public MainWindow(MainWindowViewModel viewModel)
{
InitializeComponent();
this.DataContext = viewModel;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public partial class MainWindowViewModel : ObservableObject
{
private readonly ILogger _logger;
[ObservableProperty]
private string title = "hello";
public MainWindowViewModel(ILogger logger)
{
this._logger = logger;
_logger.Information(title);
}
}
|
第二种方式(基于 .NET Generic Host,仅仅.NET Core):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| public partial class App : Application
{
private static readonly IHost _host = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration(c =>
{
c.SetBasePath(AppContext.BaseDirectory);
})
.ConfigureServices(services =>
{
services.AddHostedService<ApplicationHostService>();
services.AddSingleton<MainWindow>();
services.AddSingleton<MainWindowViewModel>();
services.AddDbContext<DefaultDbContext>();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
Log.Logger = new LoggerConfiguration()
.WriteTo.File("Log/log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
logging.AddSerilog(Log.Logger);
})
.Build();
private void OnStartup(object sender, StartupEventArgs e)
{
_host.Start();
}
private void OnExit(object sender, ExitEventArgs e)
{
_host.StopAsync().Wait();
_host.Dispose();
}
public static T GetRequiredService<T>() where T : class
{
return _host.Services.GetRequiredService<T>();
}
}
|
1
2
3
4
5
6
7
8
9
10
| <Application
x:Class="Wpf_Generic_Host.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Wpf_Generic_Host"
Startup="OnStartup"
Exit="OnExit"
>
<Application.Resources />
</Application>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| internal class ApplicationHostService : IHostedService
{
private readonly IServiceProvider _serviceProvider;
public ApplicationHostService(IServiceProvider serviceProvider)
{
this._serviceProvider = serviceProvider;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await HandleActivationAsync();
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
/// <summary>
/// Creates main window during activation.
/// </summary>
/// <returns></returns>
private Task HandleActivationAsync()
{
if (Application.Current.Windows.OfType<MainWindow>().Any())
{
return Task.CompletedTask;
}
var mainWindow = _serviceProvider.GetRequiredService<MainWindow>();
mainWindow.Loaded += MainWindow_Loaded;
mainWindow?.Show();
return Task.CompletedTask;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
if (sender is not MainWindow mainWindow)
{
return;
}
//_ = mainWindow.NavigationView.Navigate(typeof(DashboardPage));
}
}
|