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

UWP中RichEditBox设置Link时格式错位的异常行为问询

UWP RichEditBox格式异常问题解析

在UWP开发中使用RichEditBox时出现异常:当先为文本的5-10范围设置链接和加粗格式,再为25-30范围设置加粗、字号、背景色、字体及链接后,25-30的格式会错误应用到5-10的文本范围;但调换两段格式设置代码的顺序后,显示就恢复正常。

异常代码(先处理5-10选区)

public MainPage()
{
    this.InitializeComponent();
    box1 = new RichEditBox()
    {
        Width = 500,
        Height = 500,
        BorderBrush = new SolidColorBrush(Colors.Blue),
        BorderThickness = new Thickness(1, 1, 1, 1)
    };
    btn = new Button() { Content = "button" };
    btn.Click += Btn_Click;
    string str = "ppppppavaniiiiipavanlllllpavankjhgfcdsa";
    box1.Document.SetText(0, str);
    box1.TextAlignment = TextAlignment.Center;

    {
        ITextSelection selection1 = box1.Document.Selection;

        selection1.StartPosition = 5;
        selection1.EndPosition = 10;
        Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection1.StartPosition);
        Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection1.EndPosition);

        selection1.CharacterFormat.Bold = FormatEffect.On;

        string s = "\"google.com\"";
        selection1.Link = s;

        selection1.Collapse(false);
    }
    {
        ITextSelection selection2 = box1.Document.Selection;

        selection2.StartPosition = 25;
        selection2.EndPosition = 30;
        Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection2.StartPosition);
        Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection2.EndPosition);

        selection2.CharacterFormat.Bold = FormatEffect.On;
        selection2.CharacterFormat.Size = 20;
        selection2.CharacterFormat.BackgroundColor = Color.FromArgb(0, 50, 77, 98);
        selection2.CharacterFormat.Name = "Algerian";

        string s = "\"gjhgfdghje.com\"";
        selection2.Link = s;
    }

    grid.Children.Add(box1);
    grid.Children.Add(btn);
}

正常代码(先处理25-30选区)

public MainPage()
{
    this.InitializeComponent();
    box1 = new RichEditBox()
    {
        Width = 500,
        Height = 500,
        BorderBrush = new SolidColorBrush(Colors.Blue),
        BorderThickness = new Thickness(1, 1, 1, 1)
    };
    btn = new Button() { Content = "button" };
    btn.Click += Btn_Click;
    string str = "ppppppavaniiiiipavanlllllpavankjhgfcdsa";
    box1.Document.SetText(0, str);
    box1.TextAlignment = TextAlignment.Center;

    {
        ITextSelection selection2 = box1.Document.Selection;

        selection2.StartPosition = 25;
        selection2.EndPosition = 30;
        Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection2.StartPosition);
        Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection2.EndPosition);

        selection2.CharacterFormat.Bold = FormatEffect.On;
        selection2.CharacterFormat.Size = 20;
        selection2.CharacterFormat.BackgroundColor = Color.FromArgb(0, 50, 77, 98);
        selection2.CharacterFormat.Name = "Algerian";

        string s = "\"gjhgfdghje.com\"";
        selection2.Link = s;
    }
    {
        ITextSelection selection1 = box1.Document.Selection;

        selection1.StartPosition = 5;
        selection1.EndPosition = 10;
        Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection1.StartPosition);
        Debug.WriteLine("<<<<<<<<<<<<<<<<<<<<<" + selection1.EndPosition);

        selection1.CharacterFormat.Bold = FormatEffect.On;

        string s = "\"google.com\"";
        selection1.Link = s;

        selection1.Collapse(false);
    }
    grid.Children.Add(box1);
    grid.Children.Add(btn);
}

异常原因解析

这个异常的核心原因是UWP的RichEditBox中,ITextSelection是与文档绑定的单一实例,并非每次调用Document.Selection都会生成新对象。

当你先处理5-10的选区并调用Collapse(false)后,选区会折叠到原选区的结束位置(10处)。此时你再获取selection2时,其实还是同一个ITextSelection对象,虽然你修改了它的StartPosition和EndPosition到25-30,但在设置Link属性时,RichEditBox内部的格式应用逻辑会因为之前的折叠操作残留的状态,错误地将后续设置的字号、背景色等格式属性传播到之前的5-10链接选区。

而调换代码顺序后,最后处理的是5-10的选区,设置完链接后调用Collapse(false)只会影响当前选区的状态,不会干扰已经设置完成的25-30选区,因此格式可以正常应用。

内容的提问来源于stack exchange,提问作者pavan kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 15:17:39