【问题标题】:Simple implementation of a textblock or textfield or richtext that allows a URL in WPF using C#使用 C# 在 WPF 中允许 URL 的文本块或文本字段或富文本的简单实现
【发布时间】:2021-10-09 22:58:45
【问题描述】:

我查看了各种解决方案,了解如何在文本字段(使用 C# 的 WPF 实现中的富文本字段)上创建一个简单的 URL。

尝试在Clicking HyperLinks in a RichTextBox without holding down CTRL - WPFAdd 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 文件中...

标签: c# wpf


【解决方案1】:

我过去实现超链接的方式是创建一个附加属性,您可以将其添加到Hyperlink 上,该属性将处理RequestNavigate 本身。

例如:

namespace WpfDemo.Extensions
{
   public static class HyperlinkExtensions
   {
      public static bool GetIsExternal(DependencyObject obj)
      {
         return (bool)obj.GetValue(IsExternalProperty);
      }

      public static void SetIsExternal(DependencyObject obj, bool value)
      {
         obj.SetValue(IsExternalProperty, value);
      }

      public static readonly DependencyProperty IsExternalProperty =
          DependencyProperty.RegisterAttached("IsExternal", typeof(bool), typeof(HyperlinkExtensions), new UIPropertyMetadata(false, OnIsExternalChanged));

      private static void OnIsExternalChanged(object sender, DependencyPropertyChangedEventArgs args)
      {
         var hyperlink = sender as Hyperlink;

         if ((bool)args.NewValue)
            hyperlink.RequestNavigate += Hyperlink_RequestNavigate;
         else
            hyperlink.RequestNavigate -= Hyperlink_RequestNavigate;
      }

      private static void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
      {
         ProcessStartInfo psi = new()
         {
            FileName = e.Uri.AbsoluteUri,
            UseShellExecute = true,
         };

         Process.Start(psi);
         e.Handled = true;
      }
   }
}

代码定义了一个附加属性,当您添加到Hyperlink 时,它将为您处理导航到链接的逻辑。

您可以通过以下方式使用以下代码:

<Window
    x:Class="WpfDemo.MainWindow"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:helper="clr-namespace:WpfDemo.Extensions"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Grid>
        <TextBlock>
            <Hyperlink helper:HyperlinkExtensions.IsExternal="true" NavigateUri="https://google.com">
                Google Link
            </Hyperlink>
        </TextBlock>
    </Grid>
</Window>

【讨论】:

  • 感谢您的回答,我收到 2 个错误。找不到类型或命名空间名称超链接(您是否缺少 using 指令或程序集引用?第二个错误是 ProcessStartInfo 不包含文件名的定义。XAML 也出现无效,在输入 HyperLinkExternsions。
  • @learner 您需要修改命名空间以匹配您的解决方案(在 XAML 和类中)。对于第二个错误,您可以使用 Hyperlink_RequestNavigate 中的原始代码(我发布的代码是针对 .NET Core 的)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-13
  • 2022-09-02
  • 2023-03-17
相关资源
最近更新 更多