如何获取 FlowDocument 超链接来启动浏览器并转到 WPF 应用程序中的 URL?
WPF 应用程序中的以下代码创建一个超链接,其外观和行为类似于超链接,但单击时不会执行任何操作。
我需要更改什么才能在单击它时打开默认浏览器并转到指定的 URL?
替代文本http://www.deviantsart.com/upload/4fbnq2.png
XAML:
<Window x:Class="TestLink238492.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel Margin="10">
<ContentControl x:Name="MainArea"/>
</StackPanel>
</Window>
代码隐藏:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace TestLink238492
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
FlowDocumentScrollViewer fdsv = new FlowDocumentScrollViewer();
FlowDocument doc = new FlowDocument();
fdsv.Document = doc;
fdsv.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
doc.PagePadding = new Thickness(0);
Paragraph paragraph = new Paragraph();
doc.Blocks.Add(paragraph);
Run run = new Run("this is flow document text and ");
paragraph.Inlines.Add(run);
Run run2 = new Run("this is a hyperlink");
Hyperlink hlink = new Hyperlink(run2);
hlink.NavigateUri = new Uri("http://www.google.com");
paragraph.Inlines.Add(hlink);
StackPanel sp = new StackPanel();
TextBlock tb = new TextBlock();
tb.Text = "this is textblock text";
sp.Children.Add(tb);
sp.Children.Add(fdsv);
MainArea.Content = sp;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了这个问题的答案,你必须添加 RequestNavigate 并自己处理:
I found the answer to this one, you have to add RequestNavigate and handle it yourself:
找到了这个 Poma 的解决方案。下面的代码部分应添加到您需要执行此操作的类中。或者,如果您需要从多个文件中获取它,您可以将其放在某个静态类中。我已经根据我正在做的事情稍微调整了它。
您将在代码中这样调用它:
所有 HTMLConverter 内容都可以在以下位置找到:http://blogs.msdn.com/b/wpfsdk/archive/2006/05/25/606317.aspx
如果您需要将 HTML 转换为流文档。不过,这稍微超出了本主题的范围。
Got the solutions for this Poma. The code section below should be added to your class where you need to do this. Or you can put it in a static class somewhere if you need to get to it from multiple files. I've tweaked it slightly for what I'm doing.
You'll call it in your code like this:
All the HTMLConverter stuff can be found at: http://blogs.msdn.com/b/wpfsdk/archive/2006/05/25/606317.aspx
That's if you need to convert HTML to a Flow Document. Although, that's slightly out of the scope of this topic.
接受的答案对我来说不太有效。如此问题中详细介绍,
UseShellExecute = true。因此,
RequestNavigateEventHandler
将如下所示:The accepted answer did not quite work for me. As detailed in this issue,
UseShellExecute = true
needs to be included. Thus, theRequestNavigateEventHandler
will look like: