MOBILE

Windows Phone 7 Development : Using a WebBrowser Control to Display Web Content

2/4/2011 4:52:47 PM
In the first of our WebBrowser walkthroughs, we will use this control to display the contents of the web site—we will show a list of photos of the best car in the world, the Lamborghini Gallardo.
  1. With the WebBrowser control in place, it's time to add some code to initialize its content when it loads work. First, right-click MainPage.xaml in Solution Explorer and select View Code (or go directly to the MainPage.xaml.cs file).

  2. Whenever the My Car Browser application loads the WebBrowser control, it fires off a Loaded event. By creating a Loaded event handler, you can write code to display a web page with car photos. Add the following code to the MainPage() constructor to create the handler:

    webBrowser1.Loaded += new RoutedEventHandler(webBrowser1_Loaded);

Notice how the same Visual Studio shortcuts we used for the Application Bar code apply here as well: namely, right after typing "+=" Visual Studio hints that if you press the Tab key twice, it will create all of the necessary code stubs you need for a handler.

  1. Next, let's code the event handler. To the webBrowser1_Loaded() function, add the following code, which will navigate to Microsoft Bing's image search page and pass the phrase "cars Lamborghini Gallardo" to it:

    webBrowser1.Navigate(newUri("http://www.bing.com/images/search?q=cars+Lamborghini+Gallardo"",
    UriKind.Absolute));


  2. This code creates a new Uri object and specifies that the Uri is not local to our application (that would be UriKind.Relative), but rather a location on the Internet (UriKind.Absolute).

Press F5 to debug your application and see the results so far. You should see photos of the most beautiful car in the world, courtesy of the Microsoft Bing engine. You can easily extend this example to respond to user input. For example, you could use the Bing image search to show photos of any car whose name a user enters. Here's how.

  1. Now let's add a text box to the page so the user can change the name of the car for which Bing searches. To do that, go to MainPage.xaml and display the page in the Designer (either by double-clicking MainPage.xaml or by right-clicking MainPage.xaml and selecting View Designer). If the Toolbox is not visible, go to View menu => Other Windows => Toolbox or click the Toolbox icon in the Visual Studio application bar. Click and drag the text box from the Toolbox, and position it below the WebBrowser control. Next, click and drag the Button, and position it next to the text box.

  2. Right-click the text box and select Properties. Delete everything from the Text property. Next, right-click the button and change the value of its Content property to "Show It!" (without the double quotes). The end result should resemble Figure 1.

  3. It's time to add some interactivity to our application. With MainPage.xaml still open in Designer view, double-click the button. Notice how the method button1_Click opens by default when you do that, ready for your code. Place the following code in the body of that method:

    webBrowser1.Navigate(new Uri
    ("http://www.bing.com/images/search?q=cars " + textBox1.Text,
    UriKind.Absolute));

  4. Press F5 to run the application. Initially, you should see the photos of the Lamborghini Gallardo added in the first part of this walkthrough. Go ahead and type "Ford Mustang" in the text box, and press the "Show It!" button. In the WebBrowser control, you should now see a set of photos of this great American muscle car.

    Figure 1. Adding a text box and a button to interact with the WebBrowser control

But there's more. You can also use a WebBrowser control to display HTML files—and even strings—that have been stored locally. We'll use that capability to add some Help to the Car Browser application in the next section.

Other  
  •  Windows Phone 7 Development : Adding a WebBrowser Control
  •  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
  •  
    Top 10
    -  Fujifilm XF1 - The Stylish Shooter
    -  Nikon 1 V2 - Still Fast and Handles Better
    -  Asustor AS-604T 4-Bay NAS Review (Part 3)
    -  Asustor AS-604T 4-Bay NAS Review (Part 2)
    -  Asustor AS-604T 4-Bay NAS Review (Part 1)
    -  Toshiba Satellite U925t Review (Part 3)
    -  Toshiba Satellite U925t Review (Part 2)
    -  Toshiba Satellite U925t Review (Part 1)
    -  iBall Andi 4.5H - Pretty In White
    -  The HTC Butterfly - Full HD In 5 Inches Only
    Most View
    -  Editions of SQL Server 2008
    -  Access media remotely : Step-by-step access your media remotely (part 1)
    -  OS X Mountain Lion - Bringing iOS features “back to the Mac” (Part 4)
    -  Fujifilm Finepix F800EXR – Another Excellent EXR
    -  Big Shot Rivals: Sony vs. Samsung
    -  Windows Server : Planning for Migration, Upgrade, and Restructuring
    -  AOC MyStage E2343Fi 23" LED Monitor
    -  ASP.NET 3.5 Social Networking : Messaging (part 2)
    -  LaCie Porsche P'9230 - Nice Big Desktop Drive
    -  Windows Home Server Installation and Configuration
    -  Optimizing for Vertical Search : Optimizing for Local Search (part 1) - Check Your Local Listings
    -  iPad eMail : Sending multiple e-mail attachments
    -  Blackberry World 2012 (Part 3) - Mobile computing platform
    -  Programming .NET Security : Extending the .NET Framework (part 2) - Using the AuthorMembershipCondition Membership Condition
    -  Windows 7 : Fine-Tuning the Settings for Windows Media Center
    -  Evidence Explained in .NET Security
    -  Programming the iPhone : Standard Control Types (part 5) - Search Bars
    -  10 ways to save money with your PC
    -  Sharepoint 2010 : BCS Architecture - Presentation & Core Components
    -  Maintaining Windows 7 with Backup and Restore (part 2) - Using Advanced Backup Options & Using System Protection