Контекстное меню копирования и вставки не отображается при длительном нажатии для ввода, добавленного в окно во время выAndroid

Форум для тех, кто программирует под Android
Ответить Пред. темаСлед. тема
Anonymous
 Контекстное меню копирования и вставки не отображается при длительном нажатии для ввода, добавленного в окно во время вы

Сообщение Anonymous »

При длительном нажатии на запись на Android по умолчанию должно появиться контекстное меню копирования/вставки. Это работает, когда запись размещается непосредственно на странице. Однако если запись добавляется в окно при нажатии кнопки, контекстное меню копирования и вставки не отображается, а событие ContextMenuCreated собственной записи не запускается.

Код: Выделить всё

        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

/// 
/// Gets the application window.
/// 
internal static IWindow? window => GetActiveWindow();

#if ANDROID
public static PlatformView? decorViewContent => GetPlatformWindow()?.DecorView;
#endif
#endregion

#region Private methods

/// 
/// Helps to get the current active window.
/// 
///   Current active window.
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

/// 
/// Gets the activity window for Android.
/// 
/// Returns the window of the platform view.
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;
Однако событие не запускается.
Я проверил, что события LongClick и Touch запускаются для собственного представления записи.
Однако событие не запускается.
Я проверил, что события LongClick и Touch запускаются для собственного представления записи.
p>
Проверено с помощью .net maui. Проблема с записью и редактором возникает в обоих случаях.

Подробнее здесь: https://stackoverflow.com/questions/793 ... o-window-a
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Android»