what is this ambiguous behavior of RichEditBox while setting Link in UWP

57 views Asked by At
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 enter image description here

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);
        }

then the output is this enter image description here

why is this behavior is like this.

1

There are 1 answers

0
pavan kumar On BEST ANSWER

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