English 中文(简体)
在WPFRichTextBox检测超链接
原标题:Detecting hyperlinks in WPF RichTextBox

民间人士,我想写一些初步支助,用于在WPFRichTextBox控制下发现超链接。 我的计划是利用一个登记册查明任何联系,然后用真正的超链接物体人工取而代之。

然而,这一部分 在我找到联系点后,我很难找到正确的文本点等。 例如,我可以把整个文件统一到描述和找到联系的案文中,但一旦我这样做,我如何能够找到适当点,来找到需要加以补充的集团?

也许一种更好的办法是,在文件各栏目中 over碎块,假定陶瓷不会跨越多个区块,但即便如此,我与RichTextBox/FlowDocument物体模型合作的经验也很少,因此任何点人(原意)都会有所帮助。 感谢!

问题回答

我几个月来一直在努力解决这一问题。 今天,我决定请Bing Chat如何这样做。 我给出了以下方向:撰写C#代码,以扩大WPF申请中的RichTextBox控制,从而可以点击ur。

Bing提供了以下解决办法......


www.un.org/Depts/DGACM/index_french.htm 在使用C#使URLs被点击的WPF应用中,NETRichTextBox控制,你可以使用文本标识组查找URLs在案文中的开端和终结位置。 这里的一个例子是,如何为RichTextBox控制建立延伸方法:。

public static class RichTextBoxExtensions
{
    public static void MakeUrlsClickable(this RichTextBox self)
    {
        TextPointer pointer = self.Document.ContentStart;
        while (pointer != null)
        {
            if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
            {
                string textRun = pointer.GetTextInRun(LogicalDirection.Forward);
                MatchCollection matches = Regex.Matches(textRun, @"(https?://[^s]+)");
                foreach (Match match in matches)
                {
                    TextPointer start = pointer.GetPositionAtOffset(match.Index);
                    TextPointer end = start.GetPositionAtOffset(match.Length);
                    Hyperlink hyperlink = new Hyperlink(start, end);
                    hyperlink.NavigateUri = new Uri(match.Value);
                }
            }
            pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签