Skip to content

Commit f756803

Browse files
authored
Add UIWindow and UI.XAML.Window to IMauiContext (#1357)
* Add Platform Windows to IMauiContext * - window to IMauiContext * - android fix
1 parent 0bb78bf commit f756803

File tree

12 files changed

+130
-108
lines changed

12 files changed

+130
-108
lines changed

src/Compatibility/Core/src/AppHostBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder)
9999
{
100100
iOS.WillFinishLaunching((x, y) =>
101101
{
102-
MauiContext mauiContext = new MauiContext(MauiUIApplicationDelegate.Current.Services);
102+
MauiContext mauiContext = new MauiContext(MauiUIApplicationDelegate.Current.Services, new UIKit.UIWindow());
103103
Forms.Init(new ActivationState(mauiContext), new InitializationOptions() { Flags = InitializationFlags.SkipRenderers });
104104
return true;
105105
});
@@ -131,7 +131,7 @@ static IAppHostBuilder SetupDefaults(this IAppHostBuilder builder)
131131
// window and root page start creating
132132
// Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct
133133
// MauiContext inside Forms
134-
MauiContext mauiContext = new MauiContext(MauiWinUIApplication.Current.Services);
134+
MauiContext mauiContext = new MauiContext(MauiWinUIApplication.Current.Services, new UI.Xaml.Window());
135135
ActivationState state = new ActivationState(mauiContext, args);
136136
Forms.Init(state, new InitializationOptions() { Flags = InitializationFlags.SkipRenderers });
137137
})

src/Core/src/Platform/Android/MauiContext.cs

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/Core/src/Platform/IMauiContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public interface IMauiContext
1111
#if __ANDROID__
1212
global::Android.Content.Context? Context { get; }
1313
#elif __IOS__
14-
14+
UIKit.UIWindow? Window { get; }
15+
#elif WINDOWS
16+
UI.Xaml.Window? Window { get; }
1517
#endif
1618
}
1719
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Android.Content;
6+
7+
8+
namespace Microsoft.Maui
9+
{
10+
public partial class MauiContext
11+
{
12+
readonly WeakReference<Context>? _context;
13+
public MauiContext(IServiceProvider services, Context context) : this(services)
14+
{
15+
_context = new WeakReference<Context>(context ?? throw new ArgumentNullException(nameof(context)));
16+
}
17+
18+
public MauiContext(Context context) : this()
19+
{
20+
}
21+
22+
public Context? Context
23+
{
24+
get
25+
{
26+
if (_context == null)
27+
return null;
28+
29+
Context? context;
30+
if (_context.TryGetTarget(out context))
31+
{
32+
return context;
33+
}
34+
35+
return null;
36+
}
37+
}
38+
}
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Microsoft.Maui
6+
{
7+
public partial class MauiContext
8+
{
9+
public MauiContext(IServiceProvider services, UI.Xaml.Window window) : this(services)
10+
{
11+
Window = window ?? throw new ArgumentNullException(nameof(window));
12+
}
13+
14+
public UI.Xaml.Window? Window
15+
{
16+
get;
17+
private set;
18+
}
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace Microsoft.Maui
5+
{
6+
public partial class MauiContext : IMauiContext
7+
{
8+
public MauiContext()
9+
{
10+
// Temporary hack until we fully remove Forms.Init
11+
Services = null!;
12+
Handlers = null!;
13+
}
14+
15+
private MauiContext(IServiceProvider services)
16+
{
17+
Services = services ?? throw new ArgumentNullException(nameof(services));
18+
Handlers = Services.GetRequiredService<IMauiHandlersServiceProvider>();
19+
}
20+
21+
public IServiceProvider Services { get; }
22+
23+
public IMauiHandlersServiceProvider Handlers { get; }
24+
}
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using UIKit;
5+
6+
namespace Microsoft.Maui
7+
{
8+
public partial class MauiContext
9+
{
10+
readonly WeakReference<UIWindow>? _window;
11+
public MauiContext(IServiceProvider services, UIWindow window) : this(services)
12+
{
13+
_window = new WeakReference<UIWindow>(window ?? throw new ArgumentNullException(nameof(window)));
14+
}
15+
16+
17+
public UIWindow? Window
18+
{
19+
get
20+
{
21+
if (_window == null)
22+
return null;
23+
24+
UIWindow? window;
25+
if (_window.TryGetTarget(out window))
26+
{
27+
return window;
28+
}
29+
30+
return null;
31+
}
32+
}
33+
}
34+
}

src/Core/src/Platform/Windows/MauiContext.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Core/src/Platform/Windows/MauiWinUIApplication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override void OnLaunched(UI.Xaml.LaunchActivatedEventArgs args)
3232
Application = Services.GetRequiredService<IApplication>();
3333
Current.Services?.InvokeLifecycleEvents<WindowsLifecycle.OnLaunching>(del => del(this, args));
3434

35-
var mauiContext = new MauiContext(Services);
35+
var mauiContext = new MauiContext(Services, MainWindow);
3636

3737
var activationState = new ActivationState(mauiContext, args);
3838
var window = Application.CreateWindow(activationState);

src/Core/src/Platform/iOS/MauiContext.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)