Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UIWindow and UI.XAML.Window to IMauiContext #1357

Merged
merged 3 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Compatibility/Core/src/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down Expand Up @@ -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 });
})
Expand Down
44 changes: 0 additions & 44 deletions src/Core/src/Platform/Android/MauiContext.cs

This file was deleted.

4 changes: 3 additions & 1 deletion src/Core/src/Platform/IMauiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public interface IMauiContext
#if __ANDROID__
global::Android.Content.Context? Context { get; }
#elif __IOS__

UIKit.UIWindow? Window { get; }
#elif WINDOWS
UI.Xaml.Window? Window { get; }
#endif
}
}
39 changes: 39 additions & 0 deletions src/Core/src/Platform/MauiContext.Android.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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>? _context;
public MauiContext(IServiceProvider services, Context context) : this(services)
{
_context = new WeakReference<Context>(context ?? throw new ArgumentNullException(nameof(context)));
}

public MauiContext(Context context) : this()
{
}

public Context? Context
{
get
{
if (_context == null)
return null;

Context? context;
if (_context.TryGetTarget(out context))
{
return context;
}

return null;
}
}
}
}
20 changes: 20 additions & 0 deletions src/Core/src/Platform/MauiContext.Windows.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
25 changes: 25 additions & 0 deletions src/Core/src/Platform/MauiContext.cs
Original file line number Diff line number Diff line change
@@ -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<IMauiHandlersServiceProvider>();
}

public IServiceProvider Services { get; }

public IMauiHandlersServiceProvider Handlers { get; }
}
}
34 changes: 34 additions & 0 deletions src/Core/src/Platform/MauiContext.iOS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Text;
using UIKit;

namespace Microsoft.Maui
{
public partial class MauiContext
{
readonly WeakReference<UIWindow>? _window;
public MauiContext(IServiceProvider services, UIWindow window) : this(services)
{
_window = new WeakReference<UIWindow>(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;
}
}
}
}
28 changes: 0 additions & 28 deletions src/Core/src/Platform/Windows/MauiContext.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Core/src/Platform/Windows/MauiWinUIApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override void OnLaunched(UI.Xaml.LaunchActivatedEventArgs args)
Application = Services.GetRequiredService<IApplication>();
Current.Services?.InvokeLifecycleEvents<WindowsLifecycle.OnLaunching>(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);
Expand Down
27 changes: 0 additions & 27 deletions src/Core/src/Platform/iOS/MauiContext.cs

This file was deleted.

9 changes: 4 additions & 5 deletions src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l
{
Application = Services.GetRequiredService<IApplication>();

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();

Expand Down
2 changes: 2 additions & 0 deletions src/Core/tests/DeviceTests/Stubs/ContextStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}