Silverlight:- Browsing without a Browser??

Wanna navigate to Google.com from your Silverlight application?? Just put the following code

   HtmlPage.Window.Navigate(new Uri("http://www.google.co.in/")

We all know THAT…so what’s this blog all about??.Now lets take this application Out of Browser

image

image

This is what you will get..

image

WHY???:- Because there’s no browser when running out of browser, so you can’t use the DOM and Scripting.Html Bridge is disabled when your application works on Out-Of-Browser mode

So there were pretty much no direct solution to this in SL3. But in SL4 we have a brand new and shiny Web Browser control especially for OOB setting…(infact it doesn’t even work in online mode!!)

So what you need to do is this..

Check if you app is running in OOB mode using

(Application.Current.IsRunningOutOfBrowser

If running out of browser navigate to a page hosting the Web Browser control

image

In code behind navigate to the required url..

this.webBrowser.Navigate(new Uri("http://www.google.co.in/"));

and this is what you get!

image

There’s more:- Not only does this cool control let you browse to sites in an OOB mode, it also listens for events from those sites(something that doesn’t happen so easily in online mode)!!

The navigated page can then pass a value to the browsercontrol via a javascript text like

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <script type="text/javascript">
         window.external.notify('hello from google!');
   </script>
</head>
<html>

The Webbrowser has a ScriptNotify event which you can hook onto in your XAML page as

this.Browser.ScriptNotify += new EventHandler<NotifyEventArgs>(Browser_ScriptNotify);

 void Browser_ScriptNotify(object sender, NotifyEventArgs e)
        {
         string value= e.Value;
        }

Another welcome step in the direction of enabling Offline and OOB applications!!!..We recently used this for one of our projects to enable LiveID Authentication from an OOB app…pretty cool!!

Thanks

Cennest!!

Advertisements