Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

want to open Link in external browser of WP7

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.

like image 229
Shaireen Avatar asked Jan 24 '11 06:01

Shaireen


2 Answers

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();
    }
}
like image 57
decyclone Avatar answered Sep 18 '22 05:09

decyclone


You can use the WebBrowserTask to launch the browser.

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

like image 7
Jon Skeet Avatar answered Sep 19 '22 05:09

Jon Skeet