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);
}
为什么这个行为是这样的