/
WindowHandlerStub.Android.cs
57 lines (48 loc) · 1.65 KB
/
WindowHandlerStub.Android.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
using System;
using AndroidX.AppCompat.Widget;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using static Microsoft.Maui.DeviceTests.HandlerTestBase;
using AActivity = Android.App.Activity;
namespace Microsoft.Maui.DeviceTests.Stubs
{
public class WindowHandlerStub : ElementHandler<IWindow, AActivity>, IWindowHandler
{
public static IPropertyMapper<IWindow, WindowHandlerStub> WindowMapper = new PropertyMapper<IWindow, WindowHandlerStub>(WindowHandler.Mapper)
{
[nameof(IWindow.Content)] = MapContent
};
void UpdateContent()
{
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");
var rootManager = MauiContext.GetNavigationRootManager();
var previousRootView = rootManager.RootView;
rootManager.Connect(VirtualView.Content);
// This is used for cases where we are testing swapping out the page set on window
if (previousRootView?.Parent is FakeActivityRootView farw)
{
previousRootView.RemoveFromParent();
rootManager.RootView.LayoutParameters = new LinearLayoutCompat.LayoutParams(500, 500);
farw.AddView(rootManager.RootView);
}
}
public static void MapContent(WindowHandlerStub handler, IWindow window)
{
handler.UpdateContent();
}
protected override void DisconnectHandler(AActivity platformView)
{
base.DisconnectHandler(platformView);
var windowManager = MauiContext.GetNavigationRootManager();
windowManager.Disconnect();
}
public WindowHandlerStub()
: base(WindowMapper)
{
}
protected override AActivity CreatePlatformElement()
{
return MauiProgram.CurrentContext.GetActivity();
}
}
}