Код: Выделить всё
Код: Выделить всё
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
}
Код: Выделить всё
((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