簡體   English   中英

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>

文本不可點擊。

在此處輸入圖像描述

我究竟做錯了什么?

文本不可點擊。

那是因為您的Hyperlink文本甚至沒有顯示。 您會看到添加到Inlines集合的最后一個Run ,而不是超鏈接本身。

您將名為finishedUrl的相同Run添加到您的Hyperlink及其包含的TextBlock中,但您必須為Hyperlink創建一個單獨的Run實例。

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以獲得與圖像中相同的布局但帶有鏈接。

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

評論

超鏈接實現 NavigateUri 屬性,您使用單擊超鏈接時應導航到的內容的 Uri 設置該屬性。 但是,如果超鏈接的直接或間接父級是導航主機,包括 NavigationWindow、Frame 或任何可以托管 XBAP 的瀏覽器(包括 Internet Explorer 6 和更高版本,以及 Firefox 2.0+),則只能發生超鏈接導航. 有關詳細信息,請參閱導航概述中的導航主機主題。

實際上,您需要一個事件處理程序。

我不清楚您是打算出現 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);

筆記

在進程啟動后,在進程 startinfo 中沒有使用 shellexecute 的 win 10 機器上經常會出現錯誤。

在較舊的操作系統中(您會在舊的 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);   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM