From 2158889a98c2463c2368a5bb6809a1fa7046c57a Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 17 Jun 2021 19:55:16 -0500 Subject: [PATCH 1/3] Add Platform Windows to IMauiContext --- .../Core/src/AppHostBuilderExtensions.cs | 4 +- src/Core/src/Platform/Android/MauiContext.cs | 44 ------------------- src/Core/src/Platform/IMauiContext.cs | 2 +- src/Core/src/Platform/MauiContext.Android.cs | 35 +++++++++++++++ src/Core/src/Platform/MauiContext.Windows.cs | 20 +++++++++ src/Core/src/Platform/MauiContext.cs | 25 +++++++++++ src/Core/src/Platform/MauiContext.iOS.cs | 34 ++++++++++++++ src/Core/src/Platform/Windows/MauiContext.cs | 28 ------------ .../Platform/Windows/MauiWinUIApplication.cs | 2 +- src/Core/src/Platform/iOS/MauiContext.cs | 27 ------------ .../Platform/iOS/MauiUIApplicationDelegate.cs | 9 ++-- .../tests/DeviceTests/Stubs/ContextStub.cs | 2 + 12 files changed, 124 insertions(+), 108 deletions(-) delete mode 100644 src/Core/src/Platform/Android/MauiContext.cs create mode 100644 src/Core/src/Platform/MauiContext.Android.cs create mode 100644 src/Core/src/Platform/MauiContext.Windows.cs create mode 100644 src/Core/src/Platform/MauiContext.cs create mode 100644 src/Core/src/Platform/MauiContext.iOS.cs delete mode 100644 src/Core/src/Platform/Windows/MauiContext.cs delete mode 100644 src/Core/src/Platform/iOS/MauiContext.cs diff --git a/src/Compatibility/Core/src/AppHostBuilderExtensions.cs b/src/Compatibility/Core/src/AppHostBuilderExtensions.cs index 561a6c777a6f..6a6fdb06bea0 100644 --- a/src/Compatibility/Core/src/AppHostBuilderExtensions.cs +++ b/src/Compatibility/Core/src/AppHostBuilderExtensions.cs @@ -99,7 +99,7 @@ static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder) { iOS.WillFinishLaunching((x, y) => { - MauiContext mauiContext = new MauiContext(MauiUIApplicationDelegate.Current.Services); + MauiContext mauiContext = new MauiContext(MauiUIApplicationDelegate.Current.Services, new UIKit.UIWindow()); Forms.Init(new ActivationState(mauiContext), new InitializationOptions() { Flags = InitializationFlags.SkipRenderers }); return true; }); @@ -131,7 +131,7 @@ static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder) // window and root page start creating // Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct // MauiContext inside Forms - MauiContext mauiContext = new MauiContext(MauiWinUIApplication.Current.Services); + MauiContext mauiContext = new MauiContext(MauiWinUIApplication.Current.Services, new UI.Xaml.Window()); ActivationState state = new ActivationState(mauiContext, args); Forms.Init(state, new InitializationOptions() { Flags = InitializationFlags.SkipRenderers }); }) diff --git a/src/Core/src/Platform/Android/MauiContext.cs b/src/Core/src/Platform/Android/MauiContext.cs deleted file mode 100644 index 5dfad2c55f5b..000000000000 --- a/src/Core/src/Platform/Android/MauiContext.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using Android.Content; -using Microsoft.Extensions.DependencyInjection; - -namespace Microsoft.Maui -{ - public class MauiContext : IMauiContext - { - readonly WeakReference _context; - readonly IServiceProvider? _services; - readonly IMauiHandlersServiceProvider? _mauiHandlersServiceProvider; - - public MauiContext(Context context) - { - _context = new WeakReference(context ?? throw new ArgumentNullException(nameof(context))); - } - - public MauiContext(IServiceProvider services, Context context) : this(context) - { - _services = services ?? throw new ArgumentNullException(nameof(services)); - _mauiHandlersServiceProvider = Services.GetRequiredService(); - } - - public Context? Context - { - get - { - Context? context; - if (_context.TryGetTarget(out context)) - { - return context; - } - - return null; - } - } - - public IServiceProvider Services => - _services ?? throw new InvalidOperationException($"No service provider was specified during construction."); - - public IMauiHandlersServiceProvider Handlers => - _mauiHandlersServiceProvider ?? throw new InvalidOperationException($"No service provider was specified during construction."); - } -} \ No newline at end of file diff --git a/src/Core/src/Platform/IMauiContext.cs b/src/Core/src/Platform/IMauiContext.cs index a1bb999e9d68..faa1847f22aa 100644 --- a/src/Core/src/Platform/IMauiContext.cs +++ b/src/Core/src/Platform/IMauiContext.cs @@ -11,7 +11,7 @@ public interface IMauiContext #if __ANDROID__ global::Android.Content.Context? Context { get; } #elif __IOS__ - + UIKit.UIWindow? Window { get; } #endif } } diff --git a/src/Core/src/Platform/MauiContext.Android.cs b/src/Core/src/Platform/MauiContext.Android.cs new file mode 100644 index 000000000000..aa0e0c9a8fcb --- /dev/null +++ b/src/Core/src/Platform/MauiContext.Android.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.Extensions.DependencyInjection; +using Android.Content; + + +namespace Microsoft.Maui +{ + public partial class MauiContext + { + readonly WeakReference? _context; + public MauiContext(IServiceProvider services, Context context) : this(services) + { + _context = new WeakReference(context ?? throw new ArgumentNullException(nameof(context))); + } + + public Context? Context + { + get + { + if (_context == null) + return null; + + Context? context; + if (_context.TryGetTarget(out context)) + { + return context; + } + + return null; + } + } + } +} diff --git a/src/Core/src/Platform/MauiContext.Windows.cs b/src/Core/src/Platform/MauiContext.Windows.cs new file mode 100644 index 000000000000..f429f14fe7fa --- /dev/null +++ b/src/Core/src/Platform/MauiContext.Windows.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Maui +{ + public partial class MauiContext + { + public MauiContext(IServiceProvider services, UI.Xaml.Window window) : this(services) + { + Window = window ?? throw new ArgumentNullException(nameof(window)); + } + + public UI.Xaml.Window? Window + { + get; + private set; + } + } +} diff --git a/src/Core/src/Platform/MauiContext.cs b/src/Core/src/Platform/MauiContext.cs new file mode 100644 index 000000000000..db1d4a9cc5be --- /dev/null +++ b/src/Core/src/Platform/MauiContext.cs @@ -0,0 +1,25 @@ +using System; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.Maui +{ + public partial class MauiContext : IMauiContext + { + public MauiContext() + { + // Temporary hack until we fully remove Forms.Init + Services = null!; + Handlers = null!; + } + + private MauiContext(IServiceProvider services) + { + Services = services ?? throw new ArgumentNullException(nameof(services)); + Handlers = Services.GetRequiredService(); + } + + public IServiceProvider Services { get; } + + public IMauiHandlersServiceProvider Handlers { get; } + } +} \ No newline at end of file diff --git a/src/Core/src/Platform/MauiContext.iOS.cs b/src/Core/src/Platform/MauiContext.iOS.cs new file mode 100644 index 000000000000..714a9257655a --- /dev/null +++ b/src/Core/src/Platform/MauiContext.iOS.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Text; +using UIKit; + +namespace Microsoft.Maui +{ + public partial class MauiContext + { + readonly WeakReference? _window; + public MauiContext(IServiceProvider services, UIWindow window) : this(services) + { + _window = new WeakReference(window ?? throw new ArgumentNullException(nameof(window))); + } + + + public UIWindow? Window + { + get + { + if (_window == null) + return null; + + UIWindow? window; + if (_window.TryGetTarget(out window)) + { + return window; + } + + return null; + } + } + } +} diff --git a/src/Core/src/Platform/Windows/MauiContext.cs b/src/Core/src/Platform/Windows/MauiContext.cs deleted file mode 100644 index a718141fc0c7..000000000000 --- a/src/Core/src/Platform/Windows/MauiContext.cs +++ /dev/null @@ -1,28 +0,0 @@ -#nullable enable -using System; -using Microsoft.Extensions.DependencyInjection; - -namespace Microsoft.Maui -{ - public class MauiContext : IMauiContext - { - readonly IServiceProvider? _services; - readonly IMauiHandlersServiceProvider? _mauiHandlersServiceProvider; - - public MauiContext() - { - } - - public MauiContext(IServiceProvider services) - { - _services = services ?? throw new ArgumentNullException(nameof(services)); - _mauiHandlersServiceProvider = Services.GetRequiredService(); - } - - public IServiceProvider Services => - _services ?? throw new InvalidOperationException($"No service provider was specified during construction."); - - public IMauiHandlersServiceProvider Handlers => - _mauiHandlersServiceProvider ?? throw new InvalidOperationException($"No service provider was specified during construction."); - } -} \ No newline at end of file diff --git a/src/Core/src/Platform/Windows/MauiWinUIApplication.cs b/src/Core/src/Platform/Windows/MauiWinUIApplication.cs index 1d5fe4f264c9..8b6e12d3a9bc 100644 --- a/src/Core/src/Platform/Windows/MauiWinUIApplication.cs +++ b/src/Core/src/Platform/Windows/MauiWinUIApplication.cs @@ -32,7 +32,7 @@ protected override void OnLaunched(UI.Xaml.LaunchActivatedEventArgs args) Application = Services.GetRequiredService(); Current.Services?.InvokeLifecycleEvents(del => del(this, args)); - var mauiContext = new MauiContext(Services); + var mauiContext = new MauiContext(Services, MainWindow); var activationState = new ActivationState(mauiContext, args); var window = Application.CreateWindow(activationState); diff --git a/src/Core/src/Platform/iOS/MauiContext.cs b/src/Core/src/Platform/iOS/MauiContext.cs deleted file mode 100644 index 0126bd2ab8e2..000000000000 --- a/src/Core/src/Platform/iOS/MauiContext.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using Microsoft.Extensions.DependencyInjection; - -namespace Microsoft.Maui -{ - public class MauiContext : IMauiContext - { - readonly IServiceProvider? _services; - readonly IMauiHandlersServiceProvider? _mauiHandlersServiceProvider; - - public MauiContext() - { - } - - public MauiContext(IServiceProvider services) - { - _services = services ?? throw new ArgumentNullException(nameof(services)); - _mauiHandlersServiceProvider = Services.GetRequiredService(); - } - - public IServiceProvider Services => - _services ?? throw new InvalidOperationException($"No service provider was specified during construction."); - - public IMauiHandlersServiceProvider Handlers => - _mauiHandlersServiceProvider ?? throw new InvalidOperationException($"No service provider was specified during construction."); - } -} \ No newline at end of file diff --git a/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs b/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs index e083af2801bc..5c79be2a5fe8 100644 --- a/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs +++ b/src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs @@ -32,17 +32,16 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l { Application = Services.GetRequiredService(); - var mauiContext = new MauiContext(Services); + UIWindow uIWindow = new UIWindow(); + var mauiContext = new MauiContext(Services, uIWindow); var activationState = new ActivationState(mauiContext); var window = Application.CreateWindow(activationState); var page = window.View; - Window = new UIWindow - { - RootViewController = window.View.ToUIViewController(mauiContext) - }; + uIWindow.RootViewController = window.View.ToUIViewController(mauiContext); + Window = uIWindow; Window.MakeKeyAndVisible(); diff --git a/src/Core/tests/DeviceTests/Stubs/ContextStub.cs b/src/Core/tests/DeviceTests/Stubs/ContextStub.cs index 75ab5aec3c46..ed0d6d6b82da 100644 --- a/src/Core/tests/DeviceTests/Stubs/ContextStub.cs +++ b/src/Core/tests/DeviceTests/Stubs/ContextStub.cs @@ -17,6 +17,8 @@ public ContextStub(IServiceProvider services) #if __ANDROID__ public Android.Content.Context Context => Platform.DefaultContext; +#elif __IOS__ + public UIKit.UIWindow Window => throw new NotImplementedException(); #endif } } \ No newline at end of file From a345a97bff89bd5fdf9661eccccc494e617f1d71 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 17 Jun 2021 19:58:10 -0500 Subject: [PATCH 2/3] - window to IMauiContext --- src/Core/src/Platform/IMauiContext.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Core/src/Platform/IMauiContext.cs b/src/Core/src/Platform/IMauiContext.cs index faa1847f22aa..ba6f14a5b7bd 100644 --- a/src/Core/src/Platform/IMauiContext.cs +++ b/src/Core/src/Platform/IMauiContext.cs @@ -12,6 +12,8 @@ public interface IMauiContext global::Android.Content.Context? Context { get; } #elif __IOS__ UIKit.UIWindow? Window { get; } +#elif WINDOWS + UI.Xaml.Window? Window { get; } #endif } } From 4c0da3af629f81b2dc20a2982c3b20568ad7d14f Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Thu, 17 Jun 2021 23:15:23 -0500 Subject: [PATCH 3/3] - android fix --- src/Core/src/Platform/MauiContext.Android.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Core/src/Platform/MauiContext.Android.cs b/src/Core/src/Platform/MauiContext.Android.cs index aa0e0c9a8fcb..42575fdb8009 100644 --- a/src/Core/src/Platform/MauiContext.Android.cs +++ b/src/Core/src/Platform/MauiContext.Android.cs @@ -15,6 +15,10 @@ public MauiContext(IServiceProvider services, Context context) : this(services) _context = new WeakReference(context ?? throw new ArgumentNullException(nameof(context))); } + public MauiContext(Context context) : this() + { + } + public Context? Context { get