英文:
what is this ambiguous behavior of RichEditBox while setting Link in UWP
问题
The behavior you're experiencing is due to the order in which you are setting the link and formatting properties for the text ranges in your RichEditBox. In the initial code, you set the link and formatting for the range 5 to 10 before setting the properties for the range 25 to 30. However, it seems that the properties applied for the range 25 to 30 somehow get applied to the range 5 to 10 instead.
When you move the second block of code above the first block, you are first setting the properties for the range 25 to 30 and then setting the properties for the range 5 to 10. This results in the expected behavior.
The reason for this behavior is likely related to how the RichEditBox handles the selection and formatting changes. When you set the properties for a range, they might affect the current selection or the default formatting for subsequent selections. The order of operations can influence which properties are applied to which ranges.
To ensure consistent behavior, you should set the properties for your text ranges in the desired order to avoid unexpected interactions between formatting changes.
英文:
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);
}
After setting the Link
for the range 5 to 10 before setting the Link and RichText for the range 25 to 30 which some how getting applied for the range 5 to 10 instead of 25 to 30.
here is the output
If I move the second block of code above the first as below
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);
}
why is this behavior is like this.
答案1
得分: 1
I finally figured it out how RichEditBox is working while having links,
After applying a link for this range
{
ITextSelection selection1 = box1.Document.Selection;
selection1.StartPosition = 5;
selection1.EndPosition = 10;
string s = "google.com";
selection1.Link = s;
}
if we see the text of RichEditBox using box1.TextDocument.GetText(0, out txt);
then it is in this format pppppHYPERLINK "google.com"pavaniiiiipavanlllllpavankjhgfcdsa
, here the fixed characters are HYPERLINK
total 10 characters with space at the end, and we can get the link length from the
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;
}
now the final text will be in this format
pppppHYPERLINK "google.com"pavaniiiiipavanlllllHYPERLINK "google.com"pavankjhgfcdsa
英文:
i finally figured it out how RichEditBox is working while having links,
After applying link for this range
{
ITextSelection selection1 = box1.Document.Selection;
selection1.StartPosition = 5;
selection1.EndPosition = 10;
string s = "\"google.com\"";
selection1.Link = s;
}
if we see the text of RichEditBox using box1.TextDocument.GetText(0, out txt);
then it is in this format pppppHYPERLINK "google.com"pavaniiiiipavanlllllpavankjhgfcdsa
,here the fixed characters are HYPERLINK
total 10 characters with space at the end, and we can get the link length from the
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;
}
now final text will be in this format
pppppHYPERLINK "google.com"pavaniiiiipavanlllllHYPERLINK "google.com"pavankjhgfcdsa
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论