lukfi
lukfi

Reputation: 319

Content for the URI cannot be loaded. The URI may be invalid

I must say I am pretty stumped by this and I hope some of you have perhaps encountered the same issue. I have a HyperlinkButton, created like this:

var hb = new HyperlinkButton
{
     Content = fooText,
     ClickMode = ClickMode.Release,
     NavigateUri = new Uri(fooLink.Value)
};
hb.Click += hb_Click;

static void hb_Click(object sender, RoutedEventArgs e)
{
    var btn = (HyperlinkButton)sender;
    HtmlPage.Window.Navigate(btn.NavigateUri, "_blank");
}

The link is an absolute URL to a website (http://...), not inside the application. Therefore, I want to open it in a new browser tab. Which is exactly what it does! The website opens in a new tab, but in the application tab I get this error:

enter image description here

If I change the event handler so that there's no target parameter, like this:

HtmlPage.Window.Navigate(btn.NavigateUri);

...the error appears as well, just before the browser navigates away from the application. But I want to open a new tab.

I am using the Silverlight Business Application template from Visual Studio 11 and I've already noticed it has a few mystery bugs, this might be one of them. All in all, it works, I just need to get rid of the error message. But there is no exception thrown in the event handler that I could swallow.

Upvotes: 1

Views: 1433

Answers (2)

jv42
jv42

Reputation: 8593

You could do just this:

var hb = new HyperlinkButton
{
     Content = fooText,
     ClickMode = ClickMode.Release,
     NavigateUri = new Uri(fooLink.Value),
     TargetName = "_blank"
};

without the Click event handler.

It should do exactly what you want, in a cleaner way too.

Upvotes: 1

Rumplin
Rumplin

Reputation: 2768

Try it with this:

    /// <summary>
    /// Hyperlink button - simulates hyperlink click
    /// </summary>
    private class HyperlinkButtonWrapper : HyperlinkButton
    {
        public void OpenURL(string navigateUri)
        {
            OpenURL(new Uri(navigateUri, UriKind.Absolute));
        }

        public void OpenURL(Uri navigateUri)
        {
            base.NavigateUri = navigateUri;
            base.TargetName = "_blank";
            base.OnClick();
        }
    }

    /// <summary>
    /// Method opens url
    /// <para>Example: OpenURL("http://www.google.com")</para>
    /// </summary>
    /// <param name="navigateUri"></param>
    public static void OpenURL(string navigateUri)
    {
        new HyperlinkButtonWrapper().OpenURL(navigateUri);
    }

Upvotes: 0

Related Questions