[WPF] Use the Hyperlink control to create a hyperlink and jump to the browser to open the specified web page when clicked

In C# WPF, you can use the Hyperlink control to create a hyperlink and jump to the browser to open the specified web page when clicked. Here's an example:

<TextBlock>
    <Hyperlink NavigateUri="http://www.lioting.com" RequestNavigate="Hyperlink_RequestNavigate">
        www.lioting.com
    </Hyperlink>
</TextBlock>

In the above example, we used TextBlock as the container and nested a Hyperlink control inside it. The NavigateUri attribute is set to the URL of the web page, and the RequestNavigate event is used to handle the click event of the hyperlink.

Next, handle the RequestNavigate event in your code to open the browser when the hyperlink is clicked. In the window or page's code file, add the following code:

private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
    
    
    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(e.Uri.AbsoluteUri));
    e.Handled = true;
}

In the above code, we have opened a new process by using the Process.Start() method and passed the URL of the hyperlink to it as a parameter. Finally, we set e.Handled to true to prevent the default navigation behavior from being triggered.

Now, when the user clicks on the hyperlink, it will jump to the default browser and open the specified web page.

Guess you like

Origin blog.csdn.net/gao511147456/article/details/134816258