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:
 e.g. 
<phone:WebBrowser Source="http://www.mohzz.com" 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 "http://www.mohzz.com")
( 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 Microsoft.com"  (from screen 2) 


WebBrowser1.Source = new Uri("http://www.microsoft.com");
Or
WebBrowser1.Navigate(new Uri("http://www.microsoft.com"));


Now you can see the contents of "http://www.microsoft.com" ( 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