我如何知道一个TextRange是或包含一个超链接?

时间:2022-10-03 19:54:48

I tried with TextRange.GetPropertyValue(DependencyProperty), but I can't find de Dependency Property that identifies the specific range as hyperlink.

我尝试了textrang . getpropertyvalue (DependencyProperty),但是找不到de Dependency属性,它将特定范围标识为超链接。

If not with TextRange, is there anyway to find if a selected text inside a RichTextBox is a hyperlink?

如果没有TextRange,是否有办法找到RichTextBox内的选定文本是否是超链接?

Thanks :)

谢谢:)


Found an answer that is not what I was looking for but still works very well.

找到了一个不是我想要的答案,但仍然很有效的答案。

Here is the answer. It's the GetHyperlinkAncestor Method. Hope it helps other people :)

这是答案。这是GetHyperlinkAncestor方法。希望它能帮助别人。

1 个解决方案

#1


0  

The easiest way is to create a "try catch" and insert this code in the try. Hyperlink link = new Hyperlink(); link.NavigateUri = new Uri(link); If you get an exception it isn't a valid hyperlink. You just have to handle the exception so that the program wan't die.

最简单的方法是创建一个“try catch”并在try中插入此代码。超链接=新的超链接();链接。NavigateUri = new Uri(链接);如果您得到一个异常,它不是一个有效的超链接。你只需要处理异常,这样程序就不会死。

You can also do it manually by testing the string if it starts with "http://". This is done like this: `for(int i = 0; i

如果字符串以“http://”开头,还可以通过测试字符串来手动执行。这是这样做的:for(int i = 0;我

//Do something here
//And to chech the end of the link
if(link[link.lenght-1] == 'm' && link[link.lenght-2] == 'o' && link[link.length-3] == 'c'...
    //Do something

}`

} '

I hope you can use this. If not there is stil some possibilites to check the link.

我希望你能用这个。如果没有的话,仍然有可能检查链接。

#1


0  

The easiest way is to create a "try catch" and insert this code in the try. Hyperlink link = new Hyperlink(); link.NavigateUri = new Uri(link); If you get an exception it isn't a valid hyperlink. You just have to handle the exception so that the program wan't die.

最简单的方法是创建一个“try catch”并在try中插入此代码。超链接=新的超链接();链接。NavigateUri = new Uri(链接);如果您得到一个异常,它不是一个有效的超链接。你只需要处理异常,这样程序就不会死。

You can also do it manually by testing the string if it starts with "http://". This is done like this: `for(int i = 0; i

如果字符串以“http://”开头,还可以通过测试字符串来手动执行。这是这样做的:for(int i = 0;我

//Do something here
//And to chech the end of the link
if(link[link.lenght-1] == 'm' && link[link.lenght-2] == 'o' && link[link.length-3] == 'c'...
    //Do something

}`

} '

I hope you can use this. If not there is stil some possibilites to check the link.

我希望你能用这个。如果没有的话,仍然有可能检查链接。