-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
72 lines (64 loc) · 2.46 KB
/
App.xaml.cs
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Configuration;
using System.Data;
using System.Windows;
using CommunityToolkit.Mvvm.Messaging;
using HostWPF.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
namespace HostWPF
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
[STAThread]
static void Main(string[] args)
{
using var host = CreateHostBuilder(args).Build();
host.Start();
var app = new App();
app.InitializeComponent();
app.MainWindow = host.Services.GetRequiredService<MainWindow>();
app.MainWindow.Show();
app.Run();
}
private static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureServices(container =>
{
container.AddHostedService<CheckUpdateService>();
container.AddSingleton<MainWindow>(sp =>
//lambda 语句块与表达式的区别
new MainWindow()
{
DataContext = sp.GetRequiredService<MainVM>(),
});
//container.AddSingleton<MainWindow>(sp =>
//{
// //lambda 语句块与表达式的区别
// return new MainWindow() { DataContext = sp.GetRequiredService<MainVM>() };
//});
container.AddSingleton<MainVM>();
container.AddSingleton<WeakReferenceMessenger>();
container.AddSingleton<IMessenger, WeakReferenceMessenger>(provider =>
provider.GetRequiredService<WeakReferenceMessenger>()
);
container.AddSingleton(_ => Current.Dispatcher);
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
Log.Logger = new LoggerConfiguration()
.WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
//logging.AddSerilog(Log.Logger);
logging.Services.AddSingleton(Log.Logger);
});
}
}
}