.NET MAUI Android运行时添加的Entry长按无复制粘贴菜单问题
Android平台.NET MAUI中动态添加到Window的Entry长按无上下文菜单问题
在Android平台的.NET MAUI应用中,直接放置在页面上的Entry控件长按可正常弹出复制/粘贴上下文菜单,但通过按钮点击事件动态将Entry添加到Window后,长按操作无法弹出上下文菜单,且原生Entry对应的ContextMenuCreated事件未被触发。
已验证该原生视图的LongClick和Touch事件可正常触发,Entry与Editor控件均存在此问题。
相关代码
XAML代码
<VerticalStackLayout> <Button Text="Add entry to window" Clicked="OpenInWindow" HorizontalOptions="Fill" /> </VerticalStackLayout>
主页面C#代码
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
WindowOverlayHelper类代码
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 }
事件验证说明
已通过以下代码确认ContextMenuCreated事件未触发:
((Entry.Handler.PlatformView as AndroidX.AppCompat.Widget.AppCompatEditText).ContextMenuCreated += MainPage_ContextMenuCreated;
内容的提问来源于stack exchange,提问作者Anees S
相关产品推荐
相关产品推荐

