XAML 在UWP中设置链接时,RichEditBox的这种模糊行为是什么

2guxujil  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(72)
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);
        }

字符串
在为范围5到10设置Link之后,在为范围25到30设置Link和RichText之前,如何应用于范围5到10而不是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);
        }


那么输出就是这个

为什么这个行为是这样的。

mu0hgdu0

mu0hgdu01#

我终于弄明白了RichEditBox是如何在有链接的情况下工作的,在为这个范围应用链接之后

{
                ITextSelection selection1 = box1.Document.Selection;
                selection1.StartPosition = 5;
                selection1.EndPosition = 10;
                string s = "\"google.com\"";
                selection1.Link = s;
}

字符串
如果我们看到RichEditBox的文本使用box1.TextDocument.GetText(0, out txt);,那么它的格式是pppppHYPERLINK "google.com"pavaniiiiipavanlllllpavankjhgfcdsa,这里的固定字符是HYPERLINK,总共10个字符,末尾有空格,我们可以从

int prevlinklength;
{
                ITextSelection selection1 = box1.Document.Selection;
                selection1.StartPosition = 5;
                selection1.EndPosition = 10;
                prevlinklength = selection1.Link.length;
}

//now if we want to apply the link for the 25 to 30 range means 
{
                ITextSelection selection1 = box1.Document.Selection;
                selection1.StartPosition = 25 + 10 + prevlinklength;
                selection1.EndPosition = 30 + 10 + prevlinklength;
                string s = "\"google.com\"";
                selection1.Link = s;
}


现在最终文本将采用此格式

pppppHYPERLINK "google.com"pavaniiiiipavanlllllHYPERLINK "google.com"pavankjhgfcdsa

相关问题