You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI Windows端如何让Editor始终显示光标?

解决方案

要实现Windows平台MAUI Editor始终显示闪烁光标(无论是否聚焦、是否只读),可以通过自定义Handler修改原生TextBox的行为来实现,具体步骤如下:

1. 创建自定义Editor Handler

在Windows平台项目中,创建继承自EditorHandler的自定义Handler,用来修改原生TextBox的属性和事件逻辑:

using Microsoft.Maui.Handlers;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Dispatching;

namespace YourAppNamespace.Platforms.Windows;

public class PersistentCaretEditorHandler : EditorHandler
{
    protected override void ConnectHandler(TextBox platformView)
    {
        base.ConnectHandler(platformView);

        // 设置光标颜色,确保任何状态下可见
        platformView.CaretBrush = new SolidColorBrush(Microsoft.UI.Colors.Black);
        
        // 监听只读状态变化,维持光标可见性
        platformView.IsReadOnlyChanged += OnIsReadOnlyChanged;
        // 监听失去焦点事件,重新获取焦点以保留光标
        platformView.LostFocus += OnLostFocus;

        // 初始化时设置光标可见性
        UpdateCaretVisibility(platformView);
    }

    private void OnLostFocus(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
    {
        var textBox = sender as TextBox;
        // 延迟调用焦点设置,避免与其他控件的焦点事件冲突
        textBox.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal, () =>
        {
            textBox.Focus(Microsoft.UI.Xaml.FocusState.Programmatic);
        });
    }

    private void OnIsReadOnlyChanged(object sender, Microsoft.UI.Xaml.DependencyPropertyChangedEventArgs e)
    {
        var textBox = sender as TextBox;
        UpdateCaretVisibility(textBox);
    }

    private void UpdateCaretVisibility(TextBox textBox)
    {
        // 强制开启只读状态下的光标可见性
        textBox.IsReadOnlyCaretVisible = true;
        // 只读状态下确保光标颜色不被系统默认隐藏
        if (textBox.IsReadOnly)
        {
            textBox.CaretBrush = new SolidColorBrush(Microsoft.UI.Colors.Black);
        }
    }

    protected override void DisconnectHandler(TextBox platformView)
    {
        platformView.IsReadOnlyChanged -= OnIsReadOnlyChanged;
        platformView.LostFocus -= OnLostFocus;
        base.DisconnectHandler(platformView);
    }
}

2. 注册自定义Handler

在项目的MauiProgram.cs中,配置MAUI使用自定义Handler:

using YourAppNamespace.Platforms.Windows;

namespace YourAppNamespace;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            })
            .ConfigureMauiHandlers(handlers =>
            {
#if WINDOWS
                // 替换默认Editor的Handler为自定义实现
                handlers.AddHandler(typeof(Editor), typeof(PersistentCaretEditorHandler));
#endif
            });

        return builder.Build();
    }
}

关键说明

  • LostFocus事件处理:通过DispatcherQueue延迟调用Focus(),可以避免直接调用焦点时被其他控件的焦点事件覆盖,解决你之前订阅Unfocused事件无效的问题。
  • IsReadOnlyCaretVisible属性:Windows原生TextBox的该属性可强制在只读状态下显示光标,结合CaretBrush设置确保光标颜色可见。
  • 全局生效:替换默认Editor的Handler后,项目中所有Editor都会自动应用该行为,无需逐个修改。

内容的提问来源于stack exchange,提问作者Nikola Savić

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 11:27:19