When long-pressing an Entry on Android, the copy/paste context menu should appear by default. It works when the Entry is placed directly on the page. However, if the Entry is added to the window on a button click, the copy/paste context menu does not display, and the native Entry's ContextMenuCreated event is not triggered.
<VerticalStackLayout> <Button Text="Add entry to window" Clicked="OpenInWindow" HorizontalOptions="Fill" /> </VerticalStackLayout> private void OpenInWindow(object sender, EventArgs e) { Grid grid = new Grid(); grid.BackgroundColor = Colors.LightGray; grid.HeightRequest = 100; grid.Padding = 100; Entry entry = new Entry() { BackgroundColor = Colors.Yellow, Text = "Entry", HeightRequest = 100 }; grid.Children.Add(entry); entry.Loaded += Entry_Loaded; #if ANDROID IWindowManager windowManager = WindowOverlayHelper.GetPlatformWindow()!.WindowManager!; this.GetWindowManagerLayoutParams(); if (windowManager != null && WindowManagerLayoutParams != null) { IMauiContext? context = grid.Handler?.MauiContext ?? WindowOverlayHelper.window?.Handler?.MauiContext; PlatformView childView = grid.ToPlatform(context); windowManager!.AddView(childView, WindowManagerLayoutParams); } #endif } private void Entry_Loaded(object? sender, EventArgs e) { #if ANDROID ((sender as Entry).Handler.PlatformView as AndroidX.AppCompat.Widget.AppCompatEditText).ContextMenuCreated += MainPage_ContextMenuCreated; #endif } #if ANDROID private void MainPage_ContextMenuCreated(object? sender, PlatformView.CreateContextMenuEventArgs e) { System.Diagnostics.Debug.WriteLine("Context menu shown"); } public WindowManagerLayoutParams? WindowManagerLayoutParams; internal WindowManagerLayoutParams GetWindowManagerLayoutParams() { if (this.WindowManagerLayoutParams == null) { this.WindowManagerLayoutParams = new WindowManagerLayoutParams(); var decorViewContent = WindowOverlayHelper.decorViewContent; if (decorViewContent != null) { this.WindowManagerLayoutParams.Width = decorViewContent.Width; this.WindowManagerLayoutParams.Height = decorViewContent.Height; } this.WindowManagerLayoutParams.Format = Format.Translucent; } return this.WindowManagerLayoutParams; } #endif public class WindowOverlayHelper { #region Fields /// <summary> /// Gets the application window. /// </summary> internal static IWindow? window => GetActiveWindow(); #if ANDROID public static PlatformView? decorViewContent => GetPlatformWindow()?.DecorView; #endif #endregion #region Private methods /// <summary> /// Helps to get the current active window. /// </summary> /// <returns> Current active window.</returns> private static IWindow? GetActiveWindow() { var windowCollection = (IPlatformApplication.Current?.Application as Microsoft.Maui.Controls.Application)?.Windows; if (windowCollection != null) { foreach (var window in windowCollection) { if (window != null) { var propertyInfo = window.GetType().GetProperty("IsActivated", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); if (propertyInfo != null) { var isActivated = (bool)(propertyInfo?.GetValue(window))!; if (isActivated) { return window; } } } } } return (IPlatformApplication.Current?.Application as Microsoft.Maui.Controls.Application)?.MainPage?.Window; } #if ANDROID /// <summary> /// Gets the activity window for Android. /// </summary> /// <returns>Returns the window of the platform view.</returns> public static Window? GetPlatformWindow() { if (window != null && window.Handler is WindowHandler windowHandler && windowHandler.PlatformView is Activity platformActivity) { if (platformActivity == null || platformActivity.WindowManager == null || platformActivity.WindowManager.DefaultDisplay == null) { return null; } return platformActivity.Window; } return null; } #endif #endregion } I checked if the context menu is created by verifying whether the ContextMenuCreated event is triggered using:
((Entry.Handler.PlatformView as AndroidX.AppCompat.Widget.AppCompatEditText).ContextMenuCreated += MainPage_ContextMenuCreated; However, the event is not being triggered.
I verified that both the LongClick and Touch events are triggered for the Entry's native view.
Checked with .net maui Entry and Editor issue occurs in both.
OpenInWindowmethod. Such as no namespace aboutWindowOverlayHelperandthis.GetWindowManagerLayoutParams();public static PlatformView? decorViewContent => GetPlatformWindow()?.DecorView;give me the error: The type or namespace 'PlatformView' can't be found. How can you declare this variable?And it also shows the error in theOpenInWindowmethod.