From 7eeed1c84624fecb194baa5f2a312fa57844b73c Mon Sep 17 00:00:00 2001 From: Atanas Popatanasov Date: Fri, 19 Sep 2025 13:38:21 +0300 Subject: [PATCH] Use the public IsActivated property of the Window for .NET10 instead of finding it through a reflection. --- .../DeviceTests/ControlsHandlerTestBase.Android.cs | 4 ++++ .../tests/DeviceTests/ControlsHandlerTestBase.cs | 8 ++++++++ .../Stubs/MauiAppNewWindowStub.Windows.cs | 4 ++++ .../tests/DeviceTests/Stubs/WindowHandlerStub.iOS.cs | 12 ++++++++++++ 4 files changed, 28 insertions(+) diff --git a/src/Controls/tests/DeviceTests/ControlsHandlerTestBase.Android.cs b/src/Controls/tests/DeviceTests/ControlsHandlerTestBase.Android.cs index bd0e73d4ea79..508efc7c40f5 100644 --- a/src/Controls/tests/DeviceTests/ControlsHandlerTestBase.Android.cs +++ b/src/Controls/tests/DeviceTests/ControlsHandlerTestBase.Android.cs @@ -271,7 +271,11 @@ public override void OnResume() base.OnResume(); bool isCreated = _window is Window ? (bool)typeof(Window).GetProperty("IsCreated", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(_window) : false; +#if NET10_0_OR_GREATER + bool isActivated = _window is Window window ? window.IsActivated : false; +#else bool isActivated = _window is Window ? (bool)typeof(Window).GetProperty("IsActivated", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(_window) : false; +#endif if (!isCreated) _window.Created(); diff --git a/src/Controls/tests/DeviceTests/ControlsHandlerTestBase.cs b/src/Controls/tests/DeviceTests/ControlsHandlerTestBase.cs index 8b5c699341c5..13de8c909fa5 100644 --- a/src/Controls/tests/DeviceTests/ControlsHandlerTestBase.cs +++ b/src/Controls/tests/DeviceTests/ControlsHandlerTestBase.cs @@ -227,7 +227,11 @@ void OnBatchCommitted(object sender, Controls.Internals.EventArg #if !WINDOWS if (window is Window controlsWindow) { +#if NET10_0_OR_GREATER + if (!controlsWindow.IsActivated) +#else if (!(bool)typeof(Window).GetProperty("IsActivated", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(controlsWindow)) +#endif window.Activated(); } else @@ -258,7 +262,11 @@ void OnBatchCommitted(object sender, Controls.Internals.EventArg #if !WINDOWS +#if NET10_0_OR_GREATER + bool isActivated = controlsWindow?.IsActivated ?? false; +#else bool isActivated = controlsWindow == null ? false : (bool)typeof(Window).GetProperty("IsActivated", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(controlsWindow); +#endif bool isDestroyed = controlsWindow == null ? false : (bool)typeof(Window).GetProperty("IsDestroyed", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(controlsWindow); if (isActivated) diff --git a/src/Controls/tests/DeviceTests/Stubs/MauiAppNewWindowStub.Windows.cs b/src/Controls/tests/DeviceTests/Stubs/MauiAppNewWindowStub.Windows.cs index 7b93f22614b1..f69660972688 100644 --- a/src/Controls/tests/DeviceTests/Stubs/MauiAppNewWindowStub.Windows.cs +++ b/src/Controls/tests/DeviceTests/Stubs/MauiAppNewWindowStub.Windows.cs @@ -88,7 +88,11 @@ void OnActivated(object sender, UI.Xaml.WindowActivatedEventArgs args) { if (Window is not null) { +#if NET10_0_OR_GREATER + bool isActivated = Window.IsActivated; +#else bool isActivated = (bool)typeof(Window).GetProperty("IsActivated", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(Window); +#endif if (!isActivated) _window.Activated(); } diff --git a/src/Controls/tests/DeviceTests/Stubs/WindowHandlerStub.iOS.cs b/src/Controls/tests/DeviceTests/Stubs/WindowHandlerStub.iOS.cs index 113c6fb87e1a..d6ac6514fa48 100644 --- a/src/Controls/tests/DeviceTests/Stubs/WindowHandlerStub.iOS.cs +++ b/src/Controls/tests/DeviceTests/Stubs/WindowHandlerStub.iOS.cs @@ -55,7 +55,11 @@ void UpdateContent() var view = _currentView.ToPlatform2(MauiContext); if (needsToPush) { +#if NET10_0_OR_GREATER + bool fireEvents = VirtualView is Window window ? !window.IsActivated : true; +#else bool fireEvents = VirtualView is Window ? !(bool)typeof(Window).GetProperty("IsActivated", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue((Window)VirtualView) : true; +#endif var vc = ((IPlatformViewHandler)_currentView.Handler).ViewController; @@ -67,7 +71,11 @@ void UpdateContent() _workSpace.AddChildViewController(vc); _workSpace.View.AddSubview(vc.View); if (fireEvents && this is IElementHandler elementHandler && elementHandler.VirtualView is IWindow virtualView) +#if NET10_0_OR_GREATER + FireWindowEvent(virtualView, (window) => !window.IsActivated, () => +#else FireWindowEvent(virtualView, (window) => !(bool)typeof(Window).GetProperty("IsActivated", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(window), () => +#endif { if (!IsDisconnected) VirtualView.Activated(); @@ -113,7 +121,11 @@ async void Disconnect(IView view, UIWindow platformView, Action finishedClosing) return; } +#if NET10_0_OR_GREATER + FireWindowEvent(virtualView, (window) => window.IsActivated, () => virtualView.Deactivated()); +#else FireWindowEvent(virtualView, (window) => (bool)typeof(Window).GetProperty("IsActivated", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(window), () => virtualView.Deactivated()); +#endif while (pvc.PresentedViewController is UIViewController mw && typeof(Microsoft.Maui.Platform.ContentView).Assembly.GetType("Microsoft.Maui.Platform.ModalWrapper").IsInstanceOfType(mw)) {