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);
}
型
那么输出就是这个
为什么这个行为是这样的。
1条答案
按热度按时间mu0hgdu01#
我终于弄明白了RichEditBox是如何在有链接的情况下工作的,在为这个范围应用链接之后
字符串
如果我们看到RichEditBox的文本使用
box1.TextDocument.GetText(0, out txt);
,那么它的格式是pppppHYPERLINK "google.com"pavaniiiiipavanlllllpavankjhgfcdsa
,这里的固定字符是HYPERLINK
,总共10个字符,末尾有空格,我们可以从型
现在最终文本将采用此格式
型