diff --git a/src/Compatibility/ControlGallery/src/Android/Issue10182Activity.cs b/src/Compatibility/ControlGallery/src/Android/Issue10182Activity.cs index ad1312be69bc..3f16d2050187 100644 --- a/src/Compatibility/ControlGallery/src/Android/Issue10182Activity.cs +++ b/src/Compatibility/ControlGallery/src/Android/Issue10182Activity.cs @@ -30,7 +30,7 @@ // base.OnCreate(savedInstanceState); //#pragma warning disable CS0612 // Type or member is obsolete -// Forms.Init(new MauiContext(MauiApplication.Current.Services, this)); +// Forms.Init(new MauiContext(IPlatformApplication.Current.Services, this)); //#pragma warning restore CS0612 // Type or member is obsolete // LoadApplication(new Issue10182Application()); diff --git a/src/Compatibility/Core/src/AppHostBuilderExtensions.Android.cs b/src/Compatibility/Core/src/AppHostBuilderExtensions.Android.cs index 81d9a01e01fa..767362a8f82a 100644 --- a/src/Compatibility/Core/src/AppHostBuilderExtensions.Android.cs +++ b/src/Compatibility/Core/src/AppHostBuilderExtensions.Android.cs @@ -19,7 +19,7 @@ static void OnConfigureLifeCycle(IAndroidLifecycleBuilder android) // any UI has appeared. // This creates a dummy MauiContext that wraps the Application. - var services = MauiApplication.Current.Services; + var services = IPlatformApplication.Current.Services; var mauiContext = new MauiContext(services, app); var state = new ActivationState(mauiContext); #pragma warning disable CS0612 // Type or member is obsolete diff --git a/src/Compatibility/Core/src/AppHostBuilderExtensions.Tizen.cs b/src/Compatibility/Core/src/AppHostBuilderExtensions.Tizen.cs index f1ea9722b4c4..68b4d066a08a 100644 --- a/src/Compatibility/Core/src/AppHostBuilderExtensions.Tizen.cs +++ b/src/Compatibility/Core/src/AppHostBuilderExtensions.Tizen.cs @@ -19,7 +19,7 @@ static void OnConfigureLifeCycle(ITizenLifecycleBuilder tizen) // Forms.Init(). This happens before any UI has appeared. // This creates a dummy MauiContext. - var services = MauiApplication.Current.Services; + var services = IPlatformApplication.Current.Services; MauiContext mauiContext = new MauiContext(services); ActivationState state = new ActivationState(mauiContext); diff --git a/src/Compatibility/Core/src/AppHostBuilderExtensions.Windows.cs b/src/Compatibility/Core/src/AppHostBuilderExtensions.Windows.cs index 0f871f2efc43..977682d191c3 100644 --- a/src/Compatibility/Core/src/AppHostBuilderExtensions.Windows.cs +++ b/src/Compatibility/Core/src/AppHostBuilderExtensions.Windows.cs @@ -33,7 +33,7 @@ static void OnConfigureLifeCycle(IWindowsLifecycleBuilder windows) // Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct // MauiContext inside Forms - var services = MauiWinUIApplication.Current.Services; + var services = IPlatformApplication.Current?.Services ?? throw new InvalidOperationException("Unable to find Application Services"); var mauiContext = new MauiContext(services); var state = new ActivationState(mauiContext, args); #pragma warning disable CS0612 // Type or member is obsolete diff --git a/src/Compatibility/Core/src/AppHostBuilderExtensions.cs b/src/Compatibility/Core/src/AppHostBuilderExtensions.cs index a425c83983b6..2b0d15528677 100644 --- a/src/Compatibility/Core/src/AppHostBuilderExtensions.cs +++ b/src/Compatibility/Core/src/AppHostBuilderExtensions.cs @@ -125,7 +125,8 @@ public void Initialize(IServiceProvider services) #if WINDOWS var dispatcher = services.GetService() ?? - MauiWinUIApplication.Current.Services.GetRequiredService(); + IPlatformApplication.Current?.Services.GetRequiredService() ?? + throw new InvalidOperationException("Unable to find Application Services"); dispatcher.DispatchIfRequired(() => { diff --git a/src/Compatibility/Core/src/AppHostBuilderExtensions.iOS.cs b/src/Compatibility/Core/src/AppHostBuilderExtensions.iOS.cs index 199b2264cef1..cac078620e75 100644 --- a/src/Compatibility/Core/src/AppHostBuilderExtensions.iOS.cs +++ b/src/Compatibility/Core/src/AppHostBuilderExtensions.iOS.cs @@ -17,7 +17,7 @@ static void OnConfigureLifeCycle(IiOSLifecycleBuilder iOS) // Forms.Init(). This happens before any UI has appeared. // This creates a dummy MauiContext. - var services = MauiUIApplicationDelegate.Current.Services; + var services = IPlatformApplication.Current.Services; var mauiContext = new MauiContext(services); var state = new ActivationState(mauiContext); #pragma warning disable CS0612 // Type or member is obsolete diff --git a/src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs b/src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs index 9c1502e8d489..9dd5733c06da 100644 --- a/src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs +++ b/src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs @@ -198,7 +198,7 @@ public void Initialize(IServiceProvider services) #if WINDOWS var dispatcher = services.GetService() ?? - MauiWinUIApplication.Current.Services.GetRequiredService(); + IPlatformApplication.Current?.Services.GetRequiredService(); dispatcher .DispatchIfRequired(() => diff --git a/src/Core/src/Hosting/EssentialsMauiAppBuilderExtensions.cs b/src/Core/src/Hosting/EssentialsMauiAppBuilderExtensions.cs index 235866cf46f2..bcf09f49844d 100644 --- a/src/Core/src/Hosting/EssentialsMauiAppBuilderExtensions.cs +++ b/src/Core/src/Hosting/EssentialsMauiAppBuilderExtensions.cs @@ -27,7 +27,7 @@ internal static MauiAppBuilder UseEssentials(this MauiAppBuilder builder) builder.ConfigureLifecycleEvents(life => { #if __ANDROID__ - ApplicationModel.Platform.Init(MauiApplication.Current); + ApplicationModel.Platform.Init((Android.App.Application)Android.App.Application.Context); life.AddAndroid(android => android .OnCreate((activity, savedInstanceState) => diff --git a/src/Core/src/Platform/Android/MauiAppCompatActivity.Lifecycle.cs b/src/Core/src/Platform/Android/MauiAppCompatActivity.Lifecycle.cs index c79650241c3f..a63b868ffe10 100644 --- a/src/Core/src/Platform/Android/MauiAppCompatActivity.Lifecycle.cs +++ b/src/Core/src/Platform/Android/MauiAppCompatActivity.Lifecycle.cs @@ -16,7 +16,7 @@ protected override void OnActivityResult(int requestCode, Result resultCode, Int base.OnActivityResult(requestCode, resultCode, data); ActivityResultCallbackRegistry.InvokeCallback(requestCode, resultCode, data); - MauiApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this, requestCode, resultCode, data)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this, requestCode, resultCode, data)); } // TODO: Investigate whether the new AndroidX way is actually useful: @@ -27,7 +27,7 @@ public override void OnBackPressed() #pragma warning restore 809 { var preventBackPropagation = false; - MauiApplication.Current?.Services?.InvokeLifecycleEvents(del => + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => { preventBackPropagation = del(this) || preventBackPropagation; }); @@ -44,41 +44,41 @@ public override void OnConfigurationChanged(Configuration newConfig) { base.OnConfigurationChanged(newConfig); - MauiApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this, newConfig)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this, newConfig)); } protected override void OnNewIntent(Intent? intent) { base.OnNewIntent(intent); - MauiApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this, intent)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this, intent)); } protected override void OnPostCreate(Bundle? savedInstanceState) { base.OnPostCreate(savedInstanceState); - MauiApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this, savedInstanceState)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this, savedInstanceState)); } protected override void OnPostResume() { base.OnPostResume(); - MauiApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this)); } protected override void OnRestart() { base.OnRestart(); - MauiApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this)); } [System.Runtime.Versioning.SupportedOSPlatform("android23.0")] public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) { - MauiApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this, requestCode, permissions, grantResults)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this, requestCode, permissions, grantResults)); base.OnRequestPermissionsResult(requestCode, permissions, grantResults); } @@ -87,7 +87,7 @@ protected override void OnRestoreInstanceState(Bundle savedInstanceState) { base.OnRestoreInstanceState(savedInstanceState); - MauiApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this, savedInstanceState)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(this, savedInstanceState)); } } } \ No newline at end of file diff --git a/src/Core/src/Platform/Android/MauiAppCompatActivity.cs b/src/Core/src/Platform/Android/MauiAppCompatActivity.cs index 52e676937ae3..b35231c3e538 100644 --- a/src/Core/src/Platform/Android/MauiAppCompatActivity.cs +++ b/src/Core/src/Platform/Android/MauiAppCompatActivity.cs @@ -30,7 +30,10 @@ protected override void OnCreate(Bundle? savedInstanceState) base.OnCreate(savedInstanceState); - this.CreatePlatformWindow(MauiApplication.Current.Application, savedInstanceState); + if (IPlatformApplication.Current?.Application is not null) + { + this.CreatePlatformWindow(IPlatformApplication.Current.Application, savedInstanceState); + } } public override bool DispatchTouchEvent(MotionEvent? e) diff --git a/src/Core/src/Platform/Android/MauiApplication.cs b/src/Core/src/Platform/Android/MauiApplication.cs index 55c911100593..2f33449de75e 100644 --- a/src/Core/src/Platform/Android/MauiApplication.cs +++ b/src/Core/src/Platform/Android/MauiApplication.cs @@ -39,68 +39,87 @@ public override void OnCreate() var applicationContext = rootContext.MakeApplicationScope(this); - Services = applicationContext.Services; + _services = applicationContext.Services; - Current.Services?.InvokeLifecycleEvents(del => del(this)); + _services.InvokeLifecycleEvents(del => del(this)); - Application = Services.GetRequiredService(); + _application = _services.GetRequiredService(); - this.SetApplicationHandler(Application, applicationContext); + this.SetApplicationHandler(_application, applicationContext); - Current.Services?.InvokeLifecycleEvents(del => del(this)); + _services?.InvokeLifecycleEvents(del => del(this)); base.OnCreate(); } public override void OnLowMemory() { - Current.Services?.InvokeLifecycleEvents(del => del(this)); + _services?.InvokeLifecycleEvents(del => del(this)); base.OnLowMemory(); } public override void OnTrimMemory(TrimMemory level) { - Current.Services?.InvokeLifecycleEvents(del => del(this, level)); + _services?.InvokeLifecycleEvents(del => del(this, level)); base.OnTrimMemory(level); } public override void OnConfigurationChanged(Configuration newConfig) { - Current.Services?.InvokeLifecycleEvents(del => del(this, newConfig)); + _services?.InvokeLifecycleEvents(del => del(this, newConfig)); base.OnConfigurationChanged(newConfig); } public static MauiApplication Current { get; private set; } = null!; - public IServiceProvider Services { get; protected set; } = null!; + IServiceProvider? _services; - public IApplication Application { get; protected set; } = null!; + IApplication? _application; + + // TODO: we should investigate throwing an exception or changing the public API + IServiceProvider IPlatformApplication.Services => _services!; + + IApplication IPlatformApplication.Application => _application!; + + [Obsolete("Use the IPlatformApplication.Current.Services instead.")] + public IServiceProvider Services + { + get => _services!; + protected set => _services = value; + } + + [Obsolete("Use the IPlatformApplication.Current.Application instead.")] + public IApplication Application + { + get => _application!; + protected set => _application = value; + } public class ActivityLifecycleCallbacks : Java.Lang.Object, IActivityLifecycleCallbacks { public void OnActivityCreated(Activity activity, Bundle? savedInstanceState) => - Current.Services?.InvokeLifecycleEvents(del => del(activity, savedInstanceState)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(activity, savedInstanceState)); public void OnActivityStarted(Activity activity) => - Current.Services?.InvokeLifecycleEvents(del => del(activity)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(activity)); public void OnActivityResumed(Activity activity) => - Current.Services?.InvokeLifecycleEvents(del => del(activity)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(activity)); public void OnActivityPaused(Activity activity) => - Current.Services?.InvokeLifecycleEvents(del => del(activity)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(activity)); public void OnActivityStopped(Activity activity) => - Current.Services?.InvokeLifecycleEvents(del => del(activity)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(activity)); public void OnActivitySaveInstanceState(Activity activity, Bundle outState) => - Current.Services?.InvokeLifecycleEvents(del => del(activity, outState)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(activity, outState)); public void OnActivityDestroyed(Activity activity) => - Current.Services?.InvokeLifecycleEvents(del => del(activity)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(activity)); } } } \ No newline at end of file diff --git a/src/Core/src/Platform/Android/MauiContextExtensions.cs b/src/Core/src/Platform/Android/MauiContextExtensions.cs index 38c51d28624b..661229db5a76 100644 --- a/src/Core/src/Platform/Android/MauiContextExtensions.cs +++ b/src/Core/src/Platform/Android/MauiContextExtensions.cs @@ -98,8 +98,8 @@ internal static View ToPlatform( internal static IServiceProvider GetApplicationServices(this IMauiContext mauiContext) { - if (mauiContext.Context?.ApplicationContext is MauiApplication ma) - return ma.Services; + if (IPlatformApplication.Current?.Services is not null) + return IPlatformApplication.Current.Services; throw new InvalidOperationException("Unable to find Application Services"); } diff --git a/src/Core/src/Platform/Tizen/CoreAppExtensions.cs b/src/Core/src/Platform/Tizen/CoreAppExtensions.cs index cdc93ff7428c..b484ac08c935 100644 --- a/src/Core/src/Platform/Tizen/CoreAppExtensions.cs +++ b/src/Core/src/Platform/Tizen/CoreAppExtensions.cs @@ -9,7 +9,7 @@ internal static class CoreAppExtensions { public static IWindow GetWindow(this CoreApplication application) { - foreach (var window in MauiApplication.Current.Application.Windows) + foreach (var window in IPlatformApplication.Current?.Application?.Windows ?? Array.Empty()) { if (window?.Handler?.PlatformView is Window win && win == GetDefaultWindow()) return window; @@ -23,7 +23,7 @@ public static IWindow GetWindow(this CoreApplication application) if (platformWindow == null) return null; - foreach (var window in MauiApplication.Current.Application.Windows) + foreach (var window in IPlatformApplication.Current?.Application?.Windows ?? Array.Empty()) { if (window?.Handler?.PlatformView is Window win && win == platformWindow) return window; diff --git a/src/Core/src/Platform/Tizen/MauiApplication.cs b/src/Core/src/Platform/Tizen/MauiApplication.cs index ca7cec927cb7..96a57529834a 100644 --- a/src/Core/src/Platform/Tizen/MauiApplication.cs +++ b/src/Core/src/Platform/Tizen/MauiApplication.cs @@ -16,7 +16,11 @@ public abstract class MauiApplication : NUIApplication, IPlatformApplication internal Func? _handleBackButtonPressed; - IMauiContext _applicationContext = null!; + IMauiContext? _applicationContext = null!; + + IApplication? _application; + + IServiceProvider? _services; protected MauiApplication() { @@ -44,25 +48,31 @@ protected override void OnPreCreate() _applicationContext = rootContext.MakeApplicationScope(this); - Services = _applicationContext.Services; + _services = _applicationContext.Services; - if (Services == null) + if (_services == null) throw new InvalidOperationException($"The {nameof(IServiceProvider)} instance was not found."); - Current.Services.InvokeLifecycleEvents(del => del(this)); + _services.InvokeLifecycleEvents(del => del(this)); } protected override void OnCreate() { base.OnCreate(); - Application = Services.GetRequiredService(); + if (_services == null) + throw new InvalidOperationException($"The {nameof(IServiceProvider)} instance was not found."); + + if (_applicationContext == null) + throw new InvalidOperationException($"The {nameof(IMauiContext)} instance was not found."); + + _application = _services.GetRequiredService(); - this.SetApplicationHandler(Application, _applicationContext); + this.SetApplicationHandler(_application, _applicationContext); - this.CreatePlatformWindow(Application); + this.CreatePlatformWindow(_application); - Current.Services?.InvokeLifecycleEvents(del => del(this)); + _services.InvokeLifecycleEvents(del => del(this)); } public void SetBackButtonPressedHandler(Func handler) @@ -73,61 +83,76 @@ public void SetBackButtonPressedHandler(Func handler) protected override void OnAppControlReceived(AppControlReceivedEventArgs e) { base.OnAppControlReceived(e); - Current.Services?.InvokeLifecycleEvents(del => del(this, e)); + _services?.InvokeLifecycleEvents(del => del(this, e)); } protected override void OnDeviceOrientationChanged(DeviceOrientationEventArgs e) { base.OnDeviceOrientationChanged(e); - Current.Services?.InvokeLifecycleEvents(del => del(this, e)); + _services?.InvokeLifecycleEvents(del => del(this, e)); } protected override void OnLocaleChanged(LocaleChangedEventArgs e) { base.OnLocaleChanged(e); - Current.Services?.InvokeLifecycleEvents(del => del(this, e)); + _services?.InvokeLifecycleEvents(del => del(this, e)); } protected override void OnLowBattery(LowBatteryEventArgs e) { base.OnLowBattery(e); - Current.Services?.InvokeLifecycleEvents(del => del(this, e)); + _services?.InvokeLifecycleEvents(del => del(this, e)); } protected override void OnLowMemory(LowMemoryEventArgs e) { base.OnLowMemory(e); - Current.Services?.InvokeLifecycleEvents(del => del(this, e)); + _services?.InvokeLifecycleEvents(del => del(this, e)); } protected override void OnPause() { base.OnPause(); - Current.Services?.InvokeLifecycleEvents(del => del(this)); + _services?.InvokeLifecycleEvents(del => del(this)); } protected override void OnRegionFormatChanged(RegionFormatChangedEventArgs e) { base.OnRegionFormatChanged(e); - Current.Services?.InvokeLifecycleEvents(del => del(this, e)); + _services?.InvokeLifecycleEvents(del => del(this, e)); } protected override void OnResume() { base.OnResume(); - Current.Services?.InvokeLifecycleEvents(del => del(this)); + _services?.InvokeLifecycleEvents(del => del(this)); } protected override void OnTerminate() { base.OnTerminate(); - Current.Services?.InvokeLifecycleEvents(del => del(this)); + _services?.InvokeLifecycleEvents(del => del(this)); } public static new MauiApplication Current { get; private set; } = null!; - public IServiceProvider Services { get; protected set; } = null!; + // TODO: we should investigate throwing an exception or changing the public API + IServiceProvider IPlatformApplication.Services => _services!; + + IApplication IPlatformApplication.Application => _application!; - public IApplication Application { get; protected set; } = null!; + [Obsolete("Use the IPlatformApplication.Current.Services instead.")] + public IServiceProvider Services + { + get => _services!; + protected set => _services = value; + } + + [Obsolete("Use the IPlatformApplication.Current.Application instead.")] + public IApplication Application + { + get => _application!; + protected set => _application = value; + } } } diff --git a/src/Core/src/Platform/Windows/MauiContextExtensions.cs b/src/Core/src/Platform/Windows/MauiContextExtensions.cs index e5a0b6862b69..04b75919197d 100644 --- a/src/Core/src/Platform/Windows/MauiContextExtensions.cs +++ b/src/Core/src/Platform/Windows/MauiContextExtensions.cs @@ -17,7 +17,7 @@ public static UI.Xaml.Window GetPlatformWindow(this IMauiContext mauiContext) => public static IServiceProvider GetApplicationServices(this IMauiContext mauiContext) { - return MauiWinUIApplication.Current.Services + return IPlatformApplication.Current?.Services ?? throw new InvalidOperationException("Unable to find Application Services"); } diff --git a/src/Core/src/Platform/Windows/MauiWinUIApplication.cs b/src/Core/src/Platform/Windows/MauiWinUIApplication.cs index 6d5ec6a50e5d..7f0cdd3bbec7 100644 --- a/src/Core/src/Platform/Windows/MauiWinUIApplication.cs +++ b/src/Core/src/Platform/Windows/MauiWinUIApplication.cs @@ -21,10 +21,10 @@ public abstract class MauiWinUIApplication : UI.Xaml.Application, IPlatformAppli protected override void OnLaunched(UI.Xaml.LaunchActivatedEventArgs args) { // Windows running on a different thread will "launch" the app again - if (Application != null) + if (_application != null && _services != null) { - Services.InvokeLifecycleEvents(del => del(this, args)); - Services.InvokeLifecycleEvents(del => del(this, args)); + _services.InvokeLifecycleEvents(del => del(this, args)); + _services.InvokeLifecycleEvents(del => del(this, args)); return; } @@ -35,25 +35,44 @@ protected override void OnLaunched(UI.Xaml.LaunchActivatedEventArgs args) var applicationContext = rootContext.MakeApplicationScope(this); - Services = applicationContext.Services; + _services = applicationContext.Services; - Services.InvokeLifecycleEvents(del => del(this, args)); + _services.InvokeLifecycleEvents(del => del(this, args)); - Application = Services.GetRequiredService(); + _application = _services.GetRequiredService(); - this.SetApplicationHandler(Application, applicationContext); + this.SetApplicationHandler(_application, applicationContext); - this.CreatePlatformWindow(Application, args); + this.CreatePlatformWindow(_application, args); - Services.InvokeLifecycleEvents(del => del(this, args)); + _services.InvokeLifecycleEvents(del => del(this, args)); } public static new MauiWinUIApplication Current => (MauiWinUIApplication)UI.Xaml.Application.Current; public UI.Xaml.LaunchActivatedEventArgs LaunchActivatedEventArgs { get; protected set; } = null!; - public IServiceProvider Services { get; protected set; } = null!; + IServiceProvider? _services; - public IApplication Application { get; protected set; } = null!; + IApplication? _application; + + // TODO: we should investigate throwing an exception or changing the public API + IServiceProvider IPlatformApplication.Services => _services!; + + IApplication IPlatformApplication.Application => _application!; + + [Obsolete("Use the IPlatformApplication.Current.Services instead.")] + public IServiceProvider Services + { + get => _services!; + protected set => _services = value; + } + + [Obsolete("Use the IPlatformApplication.Current.Application instead.")] + public IApplication Application + { + get => _application!; + protected set => _application = value; + } } } diff --git a/src/Core/src/Platform/Windows/MauiWinUIWindow.cs b/src/Core/src/Platform/Windows/MauiWinUIWindow.cs index aad03bca73be..2e7a990c49a8 100644 --- a/src/Core/src/Platform/Windows/MauiWinUIWindow.cs +++ b/src/Core/src/Platform/Windows/MauiWinUIWindow.cs @@ -187,7 +187,7 @@ void SetIcon() internal IServiceProvider? Services => Window?.Handler?.GetServiceProvider() ?? - MauiWinUIApplication.Current.Services; + IPlatformApplication.Current?.Services; [DllImport("shell32.dll", CharSet = CharSet.Auto)] static extern IntPtr ExtractAssociatedIcon(IntPtr hInst, string iconPath, ref IntPtr index); diff --git a/src/Core/src/Platform/iOS/MauiContextExtensions.cs b/src/Core/src/Platform/iOS/MauiContextExtensions.cs index 54d529b50860..a87e9554ee41 100644 --- a/src/Core/src/Platform/iOS/MauiContextExtensions.cs +++ b/src/Core/src/Platform/iOS/MauiContextExtensions.cs @@ -15,7 +15,7 @@ public static UIWindow GetPlatformWindow(this IMauiContext mauiContext) => public static IServiceProvider GetApplicationServices(this IMauiContext mauiContext) { - return MauiUIApplicationDelegate.Current.Services ?? + return IPlatformApplication.Current?.Services ?? throw new InvalidOperationException("Unable to find Application Services"); } } diff --git a/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs b/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs index 0055eec07afd..b8d0c1e00623 100644 --- a/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs +++ b/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs @@ -19,6 +19,10 @@ public abstract partial class MauiUIApplicationDelegate : UIResponder, IUIApplic IMauiContext _applicationContext = null!; + IServiceProvider? _services; + + IApplication? _application; + protected MauiUIApplicationDelegate() : base() { Current = this; @@ -42,9 +46,9 @@ public virtual bool WillFinishLaunching(UIApplication application, NSDictionary _applicationContext = rootContext.MakeApplicationScope(this); - Services = _applicationContext.Services; + _services = _applicationContext.Services; - Services?.InvokeLifecycleEvents(del => del(application, launchOptions)); + _services?.InvokeLifecycleEvents(del => del(application, launchOptions)); return true; } @@ -52,20 +56,20 @@ public virtual bool WillFinishLaunching(UIApplication application, NSDictionary [Export("application:didFinishLaunchingWithOptions:")] public virtual bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { - Application = Services.GetRequiredService(); + _application = _services!.GetRequiredService(); - this.SetApplicationHandler(Application, _applicationContext); + this.SetApplicationHandler(_application, _applicationContext); // if there is no scene delegate or support for scene delegates, then we set up the window here if (!this.HasSceneManifest()) { - this.CreatePlatformWindow(Application, application, launchOptions); + this.CreatePlatformWindow(_application, application, launchOptions); if (Window != null) - Services?.InvokeLifecycleEvents(del => del(Window)); + _services?.InvokeLifecycleEvents(del => del(Window)); } - Services?.InvokeLifecycleEvents(del => del(application!, launchOptions!)); + _services?.InvokeLifecycleEvents(del => del(application!, launchOptions!)); return true; } @@ -88,7 +92,7 @@ public virtual UISceneConfiguration GetConfiguration(UIApplication application, [Export("application:performActionForShortcutItem:completionHandler:")] public virtual void PerformActionForShortcutItem(UIApplication application, UIApplicationShortcutItem shortcutItem, UIOperationHandler completionHandler) { - Services?.InvokeLifecycleEvents(del => del(application, shortcutItem, completionHandler)); + _services?.InvokeLifecycleEvents(del => del(application, shortcutItem, completionHandler)); } [Export("application:openURL:options:")] @@ -96,7 +100,7 @@ public virtual bool OpenUrl(UIApplication application, NSUrl url, NSDictionary o { var wasHandled = false; - Services?.InvokeLifecycleEvents(del => + _services?.InvokeLifecycleEvents(del => { wasHandled = del(application, url, options) || wasHandled; }); @@ -109,7 +113,7 @@ public virtual bool ContinueUserActivity(UIApplication application, NSUserActivi { var wasHandled = false; - Services?.InvokeLifecycleEvents(del => + _services?.InvokeLifecycleEvents(del => { wasHandled = del(application, userActivity, completionHandler) || wasHandled; }); @@ -120,43 +124,43 @@ public virtual bool ContinueUserActivity(UIApplication application, NSUserActivi [Export("applicationDidBecomeActive:")] public virtual void OnActivated(UIApplication application) { - Services?.InvokeLifecycleEvents(del => del(application)); + _services?.InvokeLifecycleEvents(del => del(application)); } [Export("applicationWillResignActive:")] public virtual void OnResignActivation(UIApplication application) { - Services?.InvokeLifecycleEvents(del => del(application)); + _services?.InvokeLifecycleEvents(del => del(application)); } [Export("applicationWillTerminate:")] public virtual void WillTerminate(UIApplication application) { - Services?.InvokeLifecycleEvents(del => del(application)); + _services?.InvokeLifecycleEvents(del => del(application)); } [Export("applicationDidEnterBackground:")] public virtual void DidEnterBackground(UIApplication application) { - Services?.InvokeLifecycleEvents(del => del(application)); + _services?.InvokeLifecycleEvents(del => del(application)); } [Export("applicationWillEnterForeground:")] public virtual void WillEnterForeground(UIApplication application) { - Services?.InvokeLifecycleEvents(del => del(application)); + _services?.InvokeLifecycleEvents(del => del(application)); } [Export("applicationSignificantTimeChange:")] public virtual void ApplicationSignificantTimeChange(UIApplication application) { - Services?.InvokeLifecycleEvents(del => del(application)); + _services?.InvokeLifecycleEvents(del => del(application)); } [Export("application:performFetchWithCompletionHandler:")] public virtual void PerformFetch(UIApplication application, Action completionHandler) { - Services?.InvokeLifecycleEvents(del => del(application, completionHandler)); + _services?.InvokeLifecycleEvents(del => del(application, completionHandler)); } [UnconditionalSuppressMessage("Memory", "MA0002", Justification = "There can only be one MauiUIApplicationDelegate.")] @@ -165,8 +169,23 @@ public virtual void PerformFetch(UIApplication application, Action _services!; + + IApplication IPlatformApplication.Application => _application!; + + [Obsolete("Use the IPlatformApplication.Current.Services instead.")] + public IServiceProvider Services + { + get => _services!; + protected set => _services = value; + } - public IApplication Application { get; protected set; } = null!; + [Obsolete("Use the IPlatformApplication.Current.Application instead.")] + public IApplication Application + { + get => _application!; + protected set => _application = value; + } } } diff --git a/src/Core/src/Platform/iOS/MauiUISceneDelegate.cs b/src/Core/src/Platform/iOS/MauiUISceneDelegate.cs index 0105a6f1d462..708e83ed022d 100644 --- a/src/Core/src/Platform/iOS/MauiUISceneDelegate.cs +++ b/src/Core/src/Platform/iOS/MauiUISceneDelegate.cs @@ -17,11 +17,11 @@ public class MauiUISceneDelegate : UIResponder, IUIWindowSceneDelegate [Export("scene:willConnectToSession:options:")] public virtual void WillConnect(UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions) { - MauiUIApplicationDelegate.Current?.Services?.InvokeLifecycleEvents(del => del(scene, session, connectionOptions)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(scene, session, connectionOptions)); - if (session.Configuration.Name == MauiUIApplicationDelegate.MauiSceneConfigurationKey && MauiUIApplicationDelegate.Current?.Application != null) + if (session.Configuration.Name == MauiUIApplicationDelegate.MauiSceneConfigurationKey && IPlatformApplication.Current?.Application != null) { - this.CreatePlatformWindow(MauiUIApplicationDelegate.Current.Application, scene, session, connectionOptions); + this.CreatePlatformWindow(IPlatformApplication.Current.Application, scene, session, connectionOptions); if (Window != null) GetServiceProvider()?.InvokeLifecycleEvents(del => del(Window)); @@ -31,7 +31,7 @@ public virtual void WillConnect(UIScene scene, UISceneSession session, UISceneCo [Export("sceneDidDisconnect:")] public virtual void DidDisconnect(UIScene scene) { - MauiUIApplicationDelegate.Current?.Services?.InvokeLifecycleEvents(del => del(scene)); + IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => del(scene)); // for iOS 13 only where active apperance is not supported yet // for iOS 14+, see DidUpdateCoordinateSpace diff --git a/src/Core/src/Platform/iOS/UIApplicationExtensions.cs b/src/Core/src/Platform/iOS/UIApplicationExtensions.cs index 6b48e5e8c454..acfb0f69e4fa 100644 --- a/src/Core/src/Platform/iOS/UIApplicationExtensions.cs +++ b/src/Core/src/Platform/iOS/UIApplicationExtensions.cs @@ -52,7 +52,7 @@ internal static UIEdgeInsets GetSafeAreaInsetsForWindow(this UIApplication appli if (platformWindow is null) return null; - foreach (var window in MauiUIApplicationDelegate.Current.Application.Windows) + foreach (var window in IPlatformApplication.Current?.Application?.Windows ?? Array.Empty()) { if (window?.Handler?.PlatformView == platformWindow) return window; diff --git a/src/TestUtils/src/DeviceTests.Runners/HeadlessRunner/Android/MauiTestInstrumentation.cs b/src/TestUtils/src/DeviceTests.Runners/HeadlessRunner/Android/MauiTestInstrumentation.cs index a4890af9faeb..206d3f719e70 100644 --- a/src/TestUtils/src/DeviceTests.Runners/HeadlessRunner/Android/MauiTestInstrumentation.cs +++ b/src/TestUtils/src/DeviceTests.Runners/HeadlessRunner/Android/MauiTestInstrumentation.cs @@ -60,7 +60,7 @@ public override async void OnStart() await _waitForApplication.Task; - Services = MauiApplication.Current.Services; + Services = IPlatformApplication.Current?.Services ?? throw new InvalidOperationException("Unable to find Application Services"); Options = Services.GetRequiredService(); RunnerOptions = Services.GetRequiredService(); diff --git a/src/TestUtils/src/DeviceTests.Runners/TestServices.cs b/src/TestUtils/src/DeviceTests.Runners/TestServices.cs index e4e58e93b795..524849c7b780 100644 --- a/src/TestUtils/src/DeviceTests.Runners/TestServices.cs +++ b/src/TestUtils/src/DeviceTests.Runners/TestServices.cs @@ -15,11 +15,11 @@ public static IServiceProvider Services if (s_services is null) { #if __ANDROID__ - s_services = MauiTestInstrumentation.Current?.Services ?? MauiApplication.Current.Services; + s_services = MauiTestInstrumentation.Current?.Services ?? IPlatformApplication.Current?.Services; #elif __IOS__ - s_services = MauiTestApplicationDelegate.Current?.Services ?? MauiUIApplicationDelegate.Current.Services; + s_services = MauiTestApplicationDelegate.Current?.Services ?? IPlatformApplication.Current?.Services; #elif WINDOWS - s_services = MauiWinUIApplication.Current.Services; + s_services = IPlatformApplication.Current?.Services; #endif }