Wednesday, May 18, 2011

Web Browser Control Windows Phone7 (WP7)

Recently I was just playing with the Web Browser in Windows Phone 7 (WP7) & got error as " You cannot call WebBrowser methods until it is in the visual tree " . 
So thought to share some basic ideas.

Once you drag and drop the WebBrowser control from tool box , you can see code below in .xaml file.
<phone:WebBrowser Name="WebBrowser1" Height="676" Width="450" />
Initially Screen looks like ( refer Screen1)

Now there are two ways to navigate to your web contents :
1. You need to update the Source property of the Web Browser control in  .xaml file:
<phone:WebBrowser Source="" Name="WebBrowser1" Height="676" Width="450" />

If we update like above then your web contents will show default contents from specified source ( i.e. contents from "")
( refer Screen2)

2. Second way is ,
 No need to update the Source property of the Web Browser control in  .xaml file:
it will remains as it is 
<phone:WebBrowser Name="WebBrowser1" Height="227" Width="460" />

Now you need Set  URI location of the Web browser control your codebase accordingly.
You can set URI location by using either "WebBrowser1.Source" property or  by calling "WebBrowser1.Navigate('uri parameter') "

e.g. Written code on Click event of button "Click to visit"  (from screen 2) 

WebBrowser1.Source = new Uri("");
WebBrowser1.Navigate(new Uri(""));

Now you can see the contents of "" ( refer Screen3 )

Hope this information will helpful for someone

I found one more interesting article regarding with How to display content, embedded within the application assembly, using the WebBrowser , By Doug Holland.

No comments:

Post a Comment