gpt4 book ai didi

c# - 如何让一个简单的超链接在 XAML 中工作?

转载 作者:行者123 更新时间:2023-11-30 21:23:09 25 4
gpt4 key购买 nike

当我运行它并点击链接时,我希望浏览器打开并转到 google,但是什么都没有发生:

<Window x:Class="TestHyperlink2343.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBlock>
You and read this or
<Hyperlink NavigateUri="http://www.google.com">go to google</Hyperlink>
etc.
</TextBlock>
</Grid>
</Window>

然后我将上面的代码替换为以下代码并且仍然没有任何反应。然而,令人惊讶的是,如果我右键单击链接,它会转到谷歌:

<Window x:Class="TestHyperlink2343.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBlock>
You and read this or
<Hyperlink MouseDown="Hyperlink_MouseDown">go to google</Hyperlink>
etc.
</TextBlock>
</Grid>
</Window>

private void Hyperlink_MouseDown(object sender, MouseButtonEventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.com");
}

为什么这些示例没有按预期工作?我如何才能让一个简单的超链接正常工作,因为用户习惯于在 Web 浏览器中工作(左键单击,打开浏览器,转到站点)?

附录

我通过创建自己的超链接解决了这个问题,没有像这样的超链接元素:

<TextBlock Text="More info at wikipedia" 
TextDecorations="Underline"
MouseDown="TextBlock_MouseDown_Wikipedia"/>

private void TextBlock_MouseDown_Wikipedia(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
System.Diagnostics.Process.Start("http://www.wikipedia.com");
}

奇怪的是超链接没有这么容易工作。

最佳答案

MSDN状态:

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 7, Microsoft Internet Explorer 6, and Firefox 2.0+). For more information, see the Navigation Hosts topic in Navigation Overview.

没有看到您的更多代码,我不知道这是否适用于您的情况。

关于c# - 如何让一个简单的超链接在 XAML 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1963532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com