MOBILE

Windows Phone 7 Development : Adding a WebBrowser Control

2/4/2011 4:51:05 PM

1. Introducing the WebBrowser Control

The most obvious reason to use the WebBrowser control is to display web content within the page of a Windows Phone 7 application. For instance, you may be developing an application that shows Twitter feeds in a portion of the screen. The easiest way to do this would be to create a WebBrowser control in the application and navigate to a given Twitter page from within that control.

Another reason to use the WebBrowser control may be to show HTML-formatted content that resides locally on Windows Phone 7. For example, if you decide to include help files with your application, the easiest way to create those files would be in the form of HTML web pages. Then, you can load those web pages in Windows Phone 7 and display them in the WebBrowser control.

Finally, you can use WebBrowser content that Windows Phone 7 application generates on the fly. That means that you can compose an HTML page dynamically in code and, without first writing that web page out to disk, display it. This is certainly a handy feature that avoids the intermediate steps of first writing an HTML file to local storage and then reading it. This feature is important when the HTML pages you want to show the user are context-sensitive: for instance, if you are developing an application that tracks basketball teams and you want to provide links to information about each individual player on the team, you will want to build your list of players based on the name of the team the user selects. Dynamic content generation allows you to do just that.

In this article, you'll learn how to use these capabilities in an application by building a simple car browser application that can search the web for photos of popular car models and display them. To get started, you first must create a main page and add some UI, including a WebBrowser control to display web and HTML content.

Before you can use the WebBrowser control to browse for the images of cars online, you need to first add the control to your application. Follow these steps to place the WebBrowser control inside your application.
  1. Create a new Windows Phone 7 Application project. Launch Visual Studio 2010 Express and select the Windows Phone Application template, then change the Project name to "WebBrowserSample," select OK, and Visual Studio will set up a new project.

  2. Click the View menu => Other Windows => Toolbox (alternately, you can also click the Toolbox icon in the Visual Studio application bar).

  3. From the Toolbox window on the left, select the WebBrowser control, click it, and drag it onto the Windows Phone 7 design surface, as shown in Figure 8-1.

  4. Position and resize the control as needed. In Figure 1, the WebBrowser control is positioned to take the upper third of the phone screen.

  5. Finally, change the name of the application from "My Application" to "My Car Browser" and change the name of the page to "Car Explorer." You can do that by double-clicking MainPage.xaml and editing the ApplicationTitleand PageTitleelements accordingly.

NOTE

You can set the Height and the Width properties to Auto (this is the default when the control is first dropped on the Windows Phone design surface and is not resized). You can also set the Horizontal Alignment and Vertical Alignment properties to Stretch, which will allow the browser window to expand as much as possible on the phone without covering other visible elements present on the phone screen.

With the WebBrowser control in place, you are now ready to look at how to use this control for each of the scenarios previously described.

Figure 1. To add the WebBrowser control to the application, drag it from the Toolbox and drop it onto MainPage.xaml on the Windows Phone 7 design surface.

Other  
  •  Programming the Mobile Web : Content Delivery (part 3)
  •  Programming the Mobile Web : Content Delivery (part 2) - File Delivery
  •  Programming the Mobile Web : Content Delivery (part 1) - Defining MIME Types
  •  iPhone Application Development : Using Switches, Segmented Controls, and Web Views (part 3)
  •  iPhone Application Development : Using Switches, Segmented Controls, and Web Views (part 2)
  •  iPhone Application Development : Using Switches, Segmented Controls, and Web Views (part 1)
  •  iPhone Application Development : Using Advanced Interface Objects and Views - User Input and Output
  •  Windows Phone 7 Development : Wiring Up Events to an Application Bar ( part 2)
  •  Windows Phone 7 Development : Wiring Up Events to an Application Bar ( part 1) - Reacting to Add Button Events
  •  Adding an Application Bar to a Windows Phone 7 Application (part 3) - Adding an Application Bar Using Managed Code
  •  Adding an Application Bar to a Windows Phone 7 Application (part 2) - Adding a Local Application Bar Using XAML & Adding Menu Items
  •  Adding an Application Bar to a Windows Phone 7 Application (part 1) - Adding Images for Use with Application Bar Buttons & Adding a Global Application Bar Using XAML
  •  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 3) - Finishing the Interface
  •  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 2) - Adding an Image View
  •  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 1)
  •  iPhone Application Development : User Input and Output
  •  Windows Phone 7 : Using Accelerometer Data to Move a Ball
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 4) - Device Libraries
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 3) - Transcoders
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 2) - Detecting the Context
  •  
    Top 10
    New Touch Controls Show Up
    Bigger Mini Meets Smaller Big Rivals (Part 2) : Amazon Kindle Fire HD, Google Nexus 7, Versus Touchpad 7
    Bigger Mini Meets Smaller Big Rivals (Part 1) : iPad Mini VS 7In Rivals, Samsung Galaxy Tab 2 7.0
    Phabulous Devices (Part 3)
    Phabulous Devices (Part 2)
    Phabulous Devices (Part 1)
    Try These Apps For The Road
    CD Players Awards – Q1 2013 (Part 2)
    CD Players Awards – Q1 2013 (Part 1)
    Home Cinema Amplifiers Awards – Q1 2013
    Most View
    Windows Server 2008 : Understanding Active Directory Sites (part 2)
    You Can Master RAW (Part 1)
    SQL Server 2008 : Developing with LINQ to SQL (part 1)
    Tips & Tricks: Keyboard Shortcuts
    Windows Phone 7 Development : Push Notifications - Implementing Cloud Service to Track Push Notifications
    Lenovo Thinkpad Xl Carbon – That’s What We’ve Been Waiting For
    Motorola Atrix 2 : Tricks With Atrix 2
    Best Photo Printers Revealed – Jan 2013 (Part 2) : Canon PIXMA MG6250
    Panasonic Super Zoom Lumix FZ200
    Excel 2013 – The Smartest Spreadsheet Software
    Microsoft XNA Game Studio 3.0 : Getting Player Input - Reading a Gamepad
    WCF Services : Data Contract - Equivalence
    Visual Studio 2010 IDE : Exporting Templates
    Speed up Linux (Part 3) - Enjoy better swappiness, The four-line speed boost
    Budget accessories to stylish PCs (Part 1) : Plantronics Marquee M155, TuneUp Utilities 2012, SteelSeries Sensei Mouse
    Windows Azure : Storing static reference data with dynamic data
    Happy iMas (Part 2)
    Programming COM+ Security (part 3) - Compiling and Installing the COM+ Application
    What Can Your Budget Buy? (Part 2)
    SQL Server 2008 : Using ADO.NET Data Services