/
MauiWinUIApplication.cs
88 lines (69 loc) · 3.12 KB
/
MauiWinUIApplication.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.LifecycleEvents;
namespace Microsoft.Maui
{
/// <summary>
/// Defines the core behavior of a .NET MAUI application running on Windows.
/// </summary>
public abstract class MauiWinUIApplication : UI.Xaml.Application, IPlatformApplication
{
/// <summary>
/// When overridden in a derived class, creates the <see cref="MauiApp"/> to be used in this application.
/// Typically a <see cref="MauiApp"/> is created by calling <see cref="MauiApp.CreateBuilder(bool)"/>, configuring
/// the returned <see cref="MauiAppBuilder"/>, and returning the built app by calling <see cref="MauiAppBuilder.Build"/>.
/// </summary>
/// <returns>The built <see cref="MauiApp"/>.</returns>
protected abstract MauiApp CreateMauiApp();
protected override void OnLaunched(UI.Xaml.LaunchActivatedEventArgs args)
{
// Windows running on a different thread will "launch" the app again
if (_application != null && _services != null)
{
_services.InvokeLifecycleEvents<WindowsLifecycle.OnLaunching>(del => del(this, args));
_services.InvokeLifecycleEvents<WindowsLifecycle.OnLaunched>(del => del(this, args));
return;
}
IPlatformApplication.Current = this;
var mauiApp = CreateMauiApp();
var rootContext = new MauiContext(mauiApp.Services);
var applicationContext = rootContext.MakeApplicationScope(this);
_services = applicationContext.Services;
_services.InvokeLifecycleEvents<WindowsLifecycle.OnLaunching>(del => del(this, args));
_application = _services.GetRequiredService<IApplication>();
this.SetApplicationHandler(_application, applicationContext);
this.CreatePlatformWindow(_application, args);
_services.InvokeLifecycleEvents<WindowsLifecycle.OnLaunched>(del => del(this, args));
}
public static new MauiWinUIApplication Current => (MauiWinUIApplication)UI.Xaml.Application.Current;
public UI.Xaml.LaunchActivatedEventArgs LaunchActivatedEventArgs { get; protected set; } = null!;
IServiceProvider? _services;
IApplication? _application;
// TODO: we should investigate throwing an exception or changing the public API
IServiceProvider IPlatformApplication.Services => _services!;
IApplication IPlatformApplication.Application => _application!;
// TODO NET9 MARK THESE AS OBSOLETE. We didn't mark them obsolete in NET8 because that
// was causing warnings to generate for our WinUI projects, so we need to workaround that
// before we mark this as obsolete.
/// <summary>
/// Use the IPlatformApplication.Current.Services instead.
/// </summary>
public IServiceProvider Services
{
get => _services!;
protected set => _services = value;
}
// TODO NET9 MARK THESE AS OBSOLETE. We didn't mark them obsolete in NET8 because that
// was causing warnings to generate for our WinUI projects, so we need to workaround that
// before we mark this as obsolete.
/// <summary>
/// Use the IPlatformApplication.Current.Application instead.
/// </summary>
public IApplication Application
{
get => _application!;
protected set => _application = value;
}
}
}