want to open Link in external browser of WP7

14,418

Solution 1

You can use the WebBrowserTask to launch the browser.

I've found that you need to escape the URL you pass to it though :(

Solution 2

Normally, you would do so using Target property on <a> tag. But, in WP7 (at least in Emulator), this does not work.

What you could do is intercept using Navigating event something like following:

void WebBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
    if (IsSupposedToOpenInPhoneBrowser(e.Uri))
    {
        e.Cancel = true;
        WebBrowserTask task = new WebBrowserTask();
        task.URL = e.Uri.ToString();
        task.Show();
    }
}

Solution 3

You can use something like that

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    WebBrowserTask webBrowserTask = new WebBrowserTask();
    webBrowserTask.Uri = new Uri("http://www.someUrl.com");
    webBrowserTask.Show(); 
}
Share:
14,418
Shaireen
Author by

Shaireen

Functional tester. Former Android Application Developer.

Updated on July 27, 2022

Comments

  • Shaireen
    Shaireen almost 2 years

    The architecture is like:
    On click of a button an HTML page opens which contains a link in it. On clicking the links I want to open it in external (default) browser of WP7 such that the application closes and link opens externally. How can I implement this?
    Added this control in xaml file:

    <phone:WebBrowser Name="browser" Margin="0,78,0,0" />
    

    On button click:

       private void Information_Loaded(Object sender,RoutedEventArgs e)
       {
           Assembly assembly = Assembly.GetExecutingAssembly();
    
           using (Stream stream = assembly.GetManifestResourceStream("index_en.html"))
           {
               using (StreamReader reader = new StreamReader(stream))
               {
                   string html = reader.ReadToEnd();
    
                   browser.NavigateToString(html);                   
               }
           }
    

    Now index_en.html has a link which is to be opened in external browser.

  • Shaireen
    Shaireen over 13 years
    How can i implement it by skipping?
  • Jon Skeet
    Jon Skeet over 13 years
    @Shaireen: Implement what by skipping? Skipping what?
  • Shaireen
    Shaireen over 13 years
    sorry for not making myself clear.Actually while using Web browser task i need to pass a url to it.So this url should be the name of html file? or is it the link that shud open?
  • Jon Skeet
    Jon Skeet over 13 years
    @Shaireen: It's the address to open. The bit you'd put in the address bar in a browser.
  • Shaireen
    Shaireen over 13 years
    Actually i have an Html file which opens on a button click.This file has the address to be opened in external browser.Now i want to capture click on that link ,only then i can open it externally?how to do that?
  • Jon Skeet
    Jon Skeet over 13 years
    @Shaireen: So how are you displaying the file? If it's in a WebBrowser control, you should hook into the Navigating event - that will tell you the URL to load. You can then cancel navigation for the control and launch the browser task instead.
  • Jon Skeet
    Jon Skeet over 13 years
    @Shaireen: No, I don't have VS handy to validate it... but I've given you enough suggestions already... and your question still isn't clear. Are you expecting the user to click on the link, or do you just want it opened immediately? If it's the latter, you'll need to find the link URL from the HTML yourself.
  • Shaireen
    Shaireen over 13 years
    Expecting a user click on th link to open it
  • Jon Skeet
    Jon Skeet over 13 years
    @Shaireen: In that case see an earlier comment - handle the Navigating event from the browser control.
  • Shaireen
    Shaireen over 13 years
    "IsSupposedToOpenInPhoneBrowser(e.Uri)" property is not found.what does it signify?
  • decyclone
    decyclone over 13 years
    It is just a fake method placeholder if you want to verify/filter Uris before opening them in phone browser... you can do without it.
  • Gaurav Sharma
    Gaurav Sharma over 10 years