Getting to Know Web Browser (Silverlight For Windows Phone)

Say you want a scenario in which you need to display a webpage but you don’t want to use your device’s built-in browser. Web Browser control is an option for this. WebBrowser control is a control that can be used to display contents in the form of a web page, whether it is a locally stored file or dynamically generated from a code.

1. If you continue from the previously made project, then add a page to learn about WebBrowser. Otherwise, create a new project for this purpose. Having been doing exercises up to this page, it should be fairly easy to do. The following example uses a previously existing project. Right click on the project, Add New Item, select Windows Phone Portrait Page and rename the file, in this example WebBrowser.xaml then select Add.

tmp1C-111

2. Change the page title into WebBrowser, add a TextBox, a button, and WebBrowser from the toolbox like the figure below.

tmp1C-112


3. Double click on the button and type in the following code:

tmp1C-113_thumb

To retrieve the web content, call for Navigate method with a URI as input parameter. For starting point, change the TextBox value into a web address.

tmp1C-114

4. Press F5 for results. This of course is very dependent to your connection speed.

tmp1C-115

5. Now let’s try to dynamically display web content from a code. Type in the code below in GO! Button’s event handler.

tmp1C-116_thumb

In this case, use NavigateToString method with an HTML string as input parameter. This string will be rendered by WebBrowser and be displayed just like any webpage.

6. Press F5 and click on the button. See the results.

tmp1C-117_thumb

A file containing HTML declaration can of course be made and stored in local storage. We have learned about using IsolatedStorage in this part.

Next post:

Previous post: