csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.2 KiB
86 lines
2.2 KiB
using System;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Platform;
|
|
|
|
namespace IntegrationTestApp.Embedding;
|
|
|
|
internal class NativeTextBox : NativeControlHost
|
|
{
|
|
private ContextMenu? _contextMenu;
|
|
private INativeTextBoxImpl? _impl;
|
|
private TextBlock _tipTextBlock;
|
|
private string _initialText = string.Empty;
|
|
|
|
public NativeTextBox()
|
|
{
|
|
_tipTextBlock = new TextBlock
|
|
{
|
|
Text = "Avalonia ToolTip",
|
|
Name = "NativeTextBoxToolTip",
|
|
};
|
|
|
|
ToolTip.SetTip(this, _tipTextBlock);
|
|
ToolTip.SetShowDelay(this, 1000);
|
|
ToolTip.SetServiceEnabled(this, false);
|
|
}
|
|
|
|
public string Text
|
|
{
|
|
get => _impl?.Text ?? _initialText;
|
|
set
|
|
{
|
|
if (_impl is not null)
|
|
_impl.Text = value;
|
|
else
|
|
_initialText = value;
|
|
}
|
|
}
|
|
|
|
public static INativeTextBoxFactory? Factory { get; set; }
|
|
|
|
protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent)
|
|
{
|
|
if (Factory is null)
|
|
return base.CreateNativeControlCore(parent);
|
|
|
|
_impl = Factory.CreateControl(parent);
|
|
_impl.Text = _initialText;
|
|
_impl.ContextMenuRequested += OnContextMenuRequested;
|
|
_impl.Hovered += OnHovered;
|
|
_impl.PointerExited += OnPointerExited;
|
|
return _impl.Handle;
|
|
}
|
|
|
|
protected override void DestroyNativeControlCore(IPlatformHandle control)
|
|
{
|
|
base.DestroyNativeControlCore(control);
|
|
}
|
|
|
|
private void OnContextMenuRequested(object? sender, EventArgs e)
|
|
{
|
|
if (_contextMenu is null)
|
|
{
|
|
var menuItem = new MenuItem { Header = "Custom Menu Item" };
|
|
menuItem.Click += (s, e) => _impl!.Text = "Context menu item clicked";
|
|
|
|
_contextMenu = new ContextMenu
|
|
{
|
|
Name = "NativeTextBoxContextMenu",
|
|
Items = { menuItem }
|
|
};
|
|
}
|
|
|
|
ToolTip.SetIsOpen(this, false);
|
|
_contextMenu.Open(this);
|
|
}
|
|
|
|
private void OnHovered(object? sender, EventArgs e)
|
|
{
|
|
ToolTip.SetIsOpen(this, true);
|
|
}
|
|
|
|
private void OnPointerExited(object? sender, EventArgs e)
|
|
{
|
|
ToolTip.SetIsOpen(this, false);
|
|
}
|
|
}
|
|
|