当前位置: 首页 > 编程语言 > C#

如何获取FlowDocument超链接以启动浏览器并转到WPF应用程序中的URL?Share

时间:2023-04-10 11:48:12 C#

如何获取FlowDocument超链接以启动浏览器并转到WPF应用程序中的URL?WPF应用程序中的以下代码创建了一个超链接,其外观和行为都类似于超链接,但在单击时不执行任何操作。我需要更改什么以便在单击它时打开默认浏览器并转到指定的url?替代文本http://www.deviantsart.com/upload/4fbnq2.pngXAML:代码隐藏:使用系统;使用System.Windows;使用System.Windows.Controls;使用System.Windows.Documents;namespaceTestLink238492{publicpartialclassWindow1:Window{publicWindow1(){InitializeComponent();FlowDocumentScrollViewerfdsv=newFlowDocumentScrollViewer();FlowDocumentdoc=newFlowDocument();fdsv.Document=doc;fdsv.VerticalScrollBarVisibility=ScrollBarVisibility.Hidden;doc=PagedPad0);段落paragraph=newParagraph();doc.Blocks.Add(段落);Runrun=newRun("thisisflowdocumenttextand");paragraph.Inlines.Add(run);runrun2=newRun("这是一个超链接");超链接hlink=newHyperlink(run2);hlink.NavigateUri=newUri("http://www.google.com");paragraph.Inlines.Add(hlink);StackPanelsp=newStackPanel();TextBlocktb=newTextBlock();tb.Text="这是文本块文本";sp.Children.Add(tb);sp.Children.Add(fdsv);MainArea.Content=sp;我找到了这个答案,你必须自己添加RequestNavigate并自己处理:Runrun2=newRun("thisisahyperlink");超链接hlink=newHyperlink(run2);hlink.NavigateUri=newUri("http://www.google.com");hlink.RequestNavigate+=newSystem.Windows.Navigation.RequestNavigateEventHandler(hlink_RequestNavigate);paragraph.Inlines.Add(hlink);voidhlink_RequestNavigate(objectsender,System.Windows.Navigation.RequestNavigateEventArgse){Process.Start(newProcessStartInfo(e.Uri.AbsoluteUri));e.Handled=true;按照这个Poma的解决方案,应该将下面的代码部分添加到您需要执行此操作的类中。或者你可以把它放在某个地方的静态类中,如果你需要从多个文件中获取它。我已经稍微调整了我正在做的事情。#region在富文本框中激活超链接//http://stackoverflow.com/questions/5465667/handle-all-hyperlinks-mouseenter-event-in-a-loaded-loose-flowdocumentvoidSubscribeToAllHyperlinks(FlowDocumentflowDocument){var超链接=GetVisuals(flowDocument).OfType();foreach(超链接中的var链接)link.RequestNavigate+=newSystem.Windows.Navigation.RequestNavigateEventHandler(link_RequestNavigate);}publicstaticIEnumerableGetVisuals(DependencyObjectroot){foreach(varchildinLogicalTreeHelper.GetChildren(root).OfType()){yieldreturnchild;foreach(vardescendantsinGetVisuals(child))yieldreturndescendants;}}voidlink_RequestNavigate(objectsender,System.Windows.Navigation.RequestNavigateEventArgse){//http://stackoverflow.com/questions/2288999/how-can-i-get-a-flowdocument-hyperlink-to-launch-浏览器和转到url-in-a-wpfProcess.Start(newProcessStartInfo(e.Uri.AbsoluteUri));e.Handled=true;}#endregion活动在富文本框中设置超链接,您可以在代码中这样称呼它:FlowDocumentflowDocument=XamlReader.Load(newXmlTextReader(newStringReader(xaml)))asFlowDocument;SubscribeToAllHyperlinks(流文档);bodyFlowDocument.Document=flowDocument;所有HTMLConverter的东西都可以在以下网址找到:http://blogs.msdn.com/b/wpfsdk/archive/2006/05/25/606317.aspx这就是将HTML转换为流文档所需的全部内容,就是这样稍微超出了本主题的范围。以上是C#学习教程:HowtogetFlowDocumenthyperlinktolaunchthebrowserandgototheURLintheWPFapplication?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: