这个在UWP中设置RichEditBox链接时的模糊行为是什么?

huangapple go评论60阅读模式
英文:

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 这个在UWP中设置RichEditBox链接时的模糊行为是什么?

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 这个在UWP中设置RichEditBox链接时的模糊行为是什么?

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

huangapple
  • 本文由 发表于 2023年5月11日 13:42:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定