如何获得一个简单的超链接在XAML中工作? [英] How to get a simple hyperlink to work in XAML?
问题描述
当我运行此链接并点击该链接时,我希望浏览器能够打开并进入谷歌,但没有会发生:
When I run this and click on the link, I would expect a browser to open and it go to google, but nothing happens:
<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>
然后我用以下内容替换上面的代码,仍然没有任何反应。但令人惊讶的是,如果我在链接上右键单击,则会转到Google:
So then I replace the above code with the following and still nothing happens. However, surprisingly, if I right click on the link, it goes to 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 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");
}
为什么这些示例不能按预期工作?我怎样才能得到一个简单的超链接工作,因为用户习惯于在网络浏览器中工作(左键单击,打开浏览器,转到网站)?
我通过创建我自己的超链接而没有这样的超链接元素解决了这个问题:
I solved this by creating my own hyperlink without the Hyperlink element like this:
<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");
}
奇怪的是超链接不能轻易完成。
Strange that hyperlink doesn't work this easily.
推荐答案
MSDN 声明:
超链接导航只能进行,但是,如果超链接的直接或间接父级是导航主机,包括 NavigationWindow ,框架,或任何可以托管XBAP的浏览器(包括Internet Explorer 7,Microsoft Internet Explorer 6和Firefox 2.0+)。有关详细信息,请参阅导航中的导航主机主题概述。
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.
如果没有看到更多代码,我不知道这是否适用于您的情况。
Without seeing more of your code I don't know whether this applies in your case or not.
这篇关于如何获得一个简单的超链接在XAML中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!