MOBILE

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

2/5/2011 5:08:03 PM
Frequently, you will want to include documentation with your application to advertise its features to users and answer their most common questions. Because of its simplicity and ubiquity, HTML, the same language used to create web pages, has become the default format for such documentation. In this section, you'll create a simple HTML page describing how to work with the car photo application that you created in the previous section. Follow these steps to create and show HTML content on Windows Phone 7.
  1. Because adding an HTML file is not an option on Windows Phone 7, you will need to add a new XML file to the project. XML files support automated syntax verification features, making it harder for you to make accidental mistakes. Right-click the WebBrowserSample project in the Solution Explorer and select Add => New Item. Then, select XML File from the list of available item types.

  2. Type the following in the newly created file (you can also copy and paste this code from the files available for download for this book).

    <html>
    <title>Web Browser Help File</title>
    <body>
    <h1>Welcome to the Windows Phone 7 Car Browser Application!
    To view the car photos, type the name of the car in the textbox and press "Show It!"
    <br/><br/>For example, "Ford Mustang"</h1>
    </body>
    </html>


  3. Save the file by pressing the Save button in Visual Studio. Next, right-click XMLFile1.xml in the Solution Explorer and click Rename. Change the name of that file to Help.htm and make sure that the Build action for that file is set to "Content" (by right-clicking and selecting Properties to bring up the Properties window).

  4. While you would expect the Help.htm file to be automatically available to the application running on Windows Phone 7, it isn't. Before it is available to your application, the Help.htm file created in the previous step needs to be available to your application in the Isolated Storage, which you can think of as disk space reserved for use by your application on Windows Phone 7. As your application loads, you'll need to copy Help.htm to an Isolated Storage location first, and then retrieve it from there for display by the WebBrowser control. For the time being, simply add the following using directives to the top of the code page and then copy into your code the SaveHelpFileToIsoStore method shown in Listing 1.

    using System.IO.IsolatedStorage;
    using System.Windows.Resources;
    using System.IO;

    Listing 1. SaveHelpFiletoIsoStore Method
    private void SaveHelpFileToIsoStore()
    {
    string strFileName = "Help.htm";
    IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

    //remove the file if exists to allow each run to independently write to
    // the Isolated Storage
    if (isoStore.FileExists(strFileName) == true)
    {
    isoStore.DeleteFile(strFileName);
    }
    StreamResourceInfo sr = Application.GetResourceStream(new Uri(strFileName,
    UriKind.Relative));
    using (BinaryReader br = new BinaryReader(sr.Stream))
    {
    byte[] data = br.ReadBytes((int)sr.Stream.Length);
    //save file to Isolated Storage
    using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(strFileName)))
    {


    bw.Write(data);
    bw.Close();
    }
    }
    }

  5. Finally, you will invoke the SaveHelpFileToIsoStore method you wrote earlier to display the contents of Help.htm in the web browser when the browser first loads. Add the call to SaveHelpFileToIsoStore in the webBrowser1_Loaded method and set the webBrowser URL to navigate to the Help.htm file, as shown here:

    void webBrowser1_Loaded(object sender, RoutedEventArgs e)
    {
    SaveHelpFileToIsoStore();
    webBrowser1.Navigate(new Uri("Help.htm", UriKind.Relative));
    }

  6. Press F5 to run the application. You should see the simple HTML Help page displayed in the WebBrowser control.

Other  
  •  Windows Mobile Security - Networking
  •  Windows Mobile Security - Local Data Storage
  •  Windows Mobile Security - Permissions and User Controls
  •  Windows Phone 7 Development : Using a WebBrowser Control to Display Web Content
  •  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)
  •  
    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