【发布时间】:2021-10-09 22:58:45
【问题描述】:
我查看了各种解决方案,了解如何在文本字段(使用 C# 的 WPF 实现中的富文本字段)上创建一个简单的 URL。
尝试在Clicking HyperLinks in a RichTextBox without holding down CTRL - WPF 和Add clickable hyperlinks to a RichTextBox without new paragraph 上实施解决方案。对于我认为应该是一项简单的任务而言,它看起来过于复杂。
<RichTextBox IsReadOnly="True" IsDocumentEnabled="True">
<FlowDocument>
<Paragraph FontSize="12"> See www.google.com</Paragraph>
</FlowDocument>
</RichTextBox>
我也尝试了链接上的实现 Example using Hyperlink in WPF
在这里,我得到的错误是这个。 MainWindow 不包含 Hyperlink_RequestNavigate 的定义,并且找不到接受 MainWindow 类型的第一个参数的可访问扩展方法 hyperlink_RequestNavigate,您是否缺少程序集引用的 using 指令。
<TextBlock>
<Hyperlink NavigateUri="http://www.google.com" RequestNavigate="Hyperlink_RequestNavigate">
Click here
</Hyperlink>
</TextBlock>
后面的代码
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
// for .NET Core you need to add UseShellExecute = true
// see https://docs.microsoft.com/dotnet/api/system.diagnostics.processstartinfo.useshellexecute#property-value
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
我希望有一个文字说明点击此处了解更多信息,如果用户点击点击此处,他们会导航到预定义的网址。
【问题讨论】:
-
您的代码似乎是有关您已链接的主题的另一个 SO 问题的 1:1 副本:stackoverflow.com/questions/10238694/… 错误消息表明您没有将该事件处理程序
Hyperlink_RequestNavigate放入文件中MainWindow.xaml.cs而是在其他一些 cs 文件中...