TextBlock 中的超链接不可点击

Hyperlink in TextBlock is is not clickable

我在代码隐藏中做了这个:

var finishedText = new Run("Some text");
var finishedUrl = new Run("http://somewhere");

var hyperlink = new Hyperlink(finishedUrl) { NavigateUri = new Uri("http://somewhere") };

hyperlink.RequestNavigate += Hyperlink_RequestNavigate;
FinishedTextBlock.Inlines.Clear();
FinishedTextBlock.Inlines.Add(finishedText);
FinishedTextBlock.Inlines.Add(hyperlink);
FinishedTextBlock.Inlines.Add(Environment.NewLine);
FinishedTextBlock.Inlines.Add(finishedUrl);

在XAML中:

<TextBlock Grid.Row="0"
           x:Name="FinishedTextBlock"
           Width="Auto"
           Margin="10 10 0 0">
</TextBlock>

文本不可点击。

我做错了什么?

The text is not clickable.

那是因为您的 Hyperlink 文字甚至没有显示。您看到添加到 Inlines 集合的最后一个 Run,而不是 hyperlink 本身。

您将相同的 Run 命名为 finishedUrl 添加到您的 Hyperlink 及其包含 TextBlock,但您必须创建一个单独的 Run 实例Hyperlink.

var finishedText = new Run("Some text");
var finishedUrl = "http://somewhere";
var finishedUrlRun = new Run(finishedUrl);

var hyperlink = new Hyperlink(finishedUrlRun) { NavigateUri = new Uri("http://somewhere") };

hyperlink.RequestNavigate += Hyperlink_RequestNavigate;
FinishedTextBlock.Inlines.Clear();
FinishedTextBlock.Inlines.Add(finishedText);
FinishedTextBlock.Inlines.Add(hyperlink);
FinishedTextBlock.Inlines.Add(Environment.NewLine);

var finishedUrlRun1 = new Run(finishedUrl);
FinishedTextBlock.Inlines.Add(finishedUrlRun1);

更好的是,不要添加最后一个 Run,因为它是多余的,并将 NewLine 替换为 LineBreak 以获得与图像中相同的布局,但是link.

FinishedTextBlock.Inlines.Clear();
FinishedTextBlock.Inlines.Add(finishedText);
FinishedTextBlock.Inlines.Add(new LineBreak());
FinishedTextBlock.Inlines.Add(hyperlink);

除非您的超链接在 webview 中,否则当您点击超链接时什么也不会发生。

您需要处理 RequestNavigate 事件并执行一些操作,然后您才能看到带有页面的 Web 浏览器出现。

如果这是框架或导航窗口中的 wpf 页面,那么它也可以在没有代码的情况下导航。

https://docs.microsoft.com/en-us/dotnet/api/system.windows.documents.hyperlink?view=netcore-3.1

Remarks

Hyperlink implements the NavigateUri property that you set with the Uri of the content that should be navigated to when the Hyperlink is clicked. Hyperlink navigation can only occur, however, if either the direct or indirect parent of a Hyperlink is a navigation host, including NavigationWindow, Frame, or any browser that can host XBAPs (which includes Internet Explorer 6 and later versions, and Firefox 2.0+). For more information, see the Navigation Hosts topic in Navigation Overview.

实际上,您需要一个事件处理程序。

我不清楚您是要显示 url 还是字符串。如果你想要一个不同的字符串,那么这应该有效:

        TextBlock tb = new TextBlock();
        tb.Inlines.Add(new Run {Text="Some text" } );
        tb.Inlines.Add(new LineBreak());
        Run linkRun = new Run("Link To Google");
        Hyperlink hyper = new Hyperlink(linkRun) { NavigateUri = new Uri(@"http://www.google.com") };
        hyper.RequestNavigate += (o, e) => Process.Start(new ProcessStartInfo(e.Uri.ToString()){ UseShellExecute = true }); 
        tb.Inlines.Add(hyper);

如果您希望显示 url 而不是其他字符串,则:

        TextBlock tb = new TextBlock();
        tb.Inlines.Add(new Run { Text = "Some text" });
        tb.Inlines.Add(new LineBreak());
        string url = @"http://www.google.com";
        Run linkRun = new Run(url);
        Hyperlink hyper = new Hyperlink(linkRun){ NavigateUri = new Uri(url) };
        hyper.RequestNavigate += (o, e) => Process.Start(new ProcessStartInfo(e.Uri.ToString()) { UseShellExecute = true });
        tb.Inlines.Add(hyper);
        sp.Children.Add(tb);

备注

一个进程启动,在win 10机器上如果没有在进程startinfo中使用shellexecute,经常会报错。

在较旧的 OS 中(您会在旧的 SO 答案中看到)您过去只能使用 URL 开始处理,并且它可以与默认浏览器一起使用。

有一个内联换行符,旨在在文本块中的一系列运行之间添加换行符。

在XAML中需要这样:

<TextBlock Grid.Row="0"
    x:Name="FinishedTextBlock"
    Width="Auto"
    Margin="10 10 0 0">
<Run Text="Some text" /> 
<LineBreak/>
<Hyperlink>http://somewhere</Hyperlink>
</TextBlock>

您代码中的超链接不包含任何内容。超链接构造函数参数 [finishedUrl] 没有初始化包含字符串“http://somewhere”的超链接。

您可以像下面这样更改您的代码:

var finishedText = new Run("Some text");
var finishedUrl = new Run("http://somewhere");

var hyperlink = new Hyperlink() { NavigateUri = new Uri("http://somewhere") };
hyperlink.Inlines.Add(finishedUrl.Text);

hyperlink.RequestNavigate += Hyperlink_RequestNavigate;
FinishedTextBlock.Inlines.Clear();
FinishedTextBlock.Inlines.Add(finishedText);
FinishedTextBlock.Inlines.Add(Environment.NewLine);
FinishedTextBlock.Inlines.Add(hyperlink);