开发者

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>

So then I replace the above code with the following and still nothing happens. How开发者_运维百科ever, 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");
}

Why don't these examples work as expected and how can I get a simple hyperlink to work as users are accustomed to them working in web browsers (left click, open browser, go to site)?

Addendum

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.


The MSDN states:

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.


You were close. I believe all you needed to do different was specify the left-mouse click specifically-

MouseLeftButtonDown

This has a lot of good info-

https://stackoverflow.com/a/10667643/3692082

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜