基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-04 ddd6889459bccf5015cd0c9629cdf4f4ce8fb8e1
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
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Moli.Desktop.Configuration;
using Moli.Desktop.Host;
using Moli.Desktop.Management;
using Moli.Desktop.Runtime;
using Moli.Desktop.Tray;
 
namespace Moli.Desktop;
 
/// <summary>Application entry point and composition root.</summary>
public partial class App : System.Windows.Application
{
    private IHost? _host;
 
    protected override async void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
 
        _host = DesktopHostBuilder.Create(e.Args).Build();
        await _host.StartAsync();
        await _host.Services.GetRequiredService<IDefaultContentInstaller>().InstallAsync();
        await _host.Services.GetRequiredService<IDesktopRuntime>().StartAsync();
        await _host.Services.GetRequiredService<ITrayShell>().StartAsync();
 
        var settings = await _host.Services.GetRequiredService<IConfigurationService>().LoadAppSettingsAsync();
        if (!settings.Startup.StartMinimized)
        {
            _host.Services.GetRequiredService<ManagementWindow>().Show();
        }
    }
 
    protected override async void OnExit(ExitEventArgs e)
    {
        if (_host is not null)
        {
            await _host.Services.GetRequiredService<ITrayShell>().StopAsync();
            await _host.Services.GetRequiredService<IDesktopRuntime>().ShutdownAsync();
            await _host.StopAsync(TimeSpan.FromSeconds(5));
            _host.Dispose();
        }
 
        base.OnExit(e);
    }
}