MOBILE

Windows Phone 7 Development : WebBrowser Control - Saving Web Pages Locally

2/8/2011 4:23:52 PM
You can also save the contents of web sites and web pages to a Windows Phone 7 as strings of HTML commands, using isolated storage and the SaveToString method of the WebBrowser control. This approach saves only the HTML on a page (of course, you probably already guessed that from the name of the method!) and ignores its images and CSS files. 

The next walkthrough will show you how to save an HTML web page locally and then load it at a later time.

  1. Open the WebBrowserSample project and bring up MainPage.xaml in the design window.

  2. Add two buttons to the Windows Phone 7 design surface. Change the Content property of the top button to "save to local storage." Change the Content property of the bottom button to "load saved content."

  3. Make sure to change the names of both buttons. You can change the name in the Properties window by clicking next to the "Button" text. Name the top button "btnSave" and name the bottom button "btnLoad".

  4. Next, let's write the event handler code for the Save button click. Double-click the top button to bring up MainPage.xaml.cs in the code view. Change the btnSave_Click method to be identical to the following:

    private void btnSave_Click(object sender, RoutedEventArgs e)
    {
    string strWebContent = webBrowser1.SaveToString();
    SaveStringToIsoStore(strWebContent);
    }

  5. Next comes event handler code to load the previously saved web page. Double-click the bottom button and make the btnLoad_Click method look like the code block here.

    private void btnLoad_Click(object sender, RoutedEventArgs e)
    {
    webBrowser1.Navigate(new Uri("web.htm", UriKind.Relative));
    }

  6. We need to add the implementation of the SaveStringToIsoStore method that will perform the actual save of the HTML string to a file in the local storage.

    private void SaveStringToIsoStore(string strWebContent)
    {
    IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

    //remove the file if exists to allow each run to independently write to
    // the Isolated Storage
    if (isoStore.FileExists("web.htm") == true)
    {
    isoStore.DeleteFile("web.htm");
    }



    StreamResourceInfo sr = new StreamResourceInfo(new
    MemoryStream(System.Text.Encoding.UTF8.GetBytes(strWebContent)), "html/text");
    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("web.htm")))
    {
    bw.Write(data);
    bw.Close();
    }
    }
    }


  7. Make sure the button1_Click event looks identical to the one here:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
    webBrowser1.Navigate(new Uri
    ("http://www.bing.com/images/search?q=cars " + textBox1.Text,
    UriKind.Absolute));
    }

  8. You are now ready to test the application. Press F5 to run it, type "Ford Mustang" in the text box, and press the "Show It!" button. Photos of the Ford Mustang should appear in the browser window. Next, press "Save Content to Isolated Storage" button. Then, erase the word "Mustang" from the text box, leaving only "Ford" and pressing "Show It!" Our friendly reminder that we need to provide more information pops up. Finally, press "Load Saved Content" to show the (distorted) thumbnails of the Ford Mustang.

Notice that the content is distorted since only HTML of the web page is saved. Many CSS stylesheets that control positioning of the elements and their look are not persisted as part of the SaveToString method.

Other  
  •  Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 3)
  •  Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 2) - Google Gears
  •  Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 1) - W3C Geolocation API
  •  Programming the Mobile Web : Geolocation and Maps - Location Techniques
  •  iPhone Programming : Table-View-Based Applications - Connecting the Controller to the Model
  •  Programming the Mobile Web : Mobilizing WordPress and Other CMSs
  •  Programming the Mobile Web : Server-Side Browser Detection and Content Delivery - Content Adaptation
  •  Programming the Mobile Web : Multimedia and Streaming
  •  Mobile Application Security : BlackBerry Security - Development and Security Testing
  •  Mobile Application Security : BlackBerry Security - Introduction to Platform
  •  Windows Phone 7 Development : Using a WebBrowser Control to Display Dynamic Content
  •  Windows Phone 7 Development : Using a WebBrowser Control to Display Local HTML Content
  •  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
  •  
    Top 10
    Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
    Canon Powershot D20 - Super-Durable Waterproof Camera
    Fujifilm Finepix F800EXR – Another Excellent EXR
    Sony NEX-6 – The Best Compact Camera
    Teufel Cubycon 2 – An Excellent All-In-One For Films
    Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
    Philips 55PFL6007T With Fantastic Picture Quality
    Philips Gioco 278G4 – An Excellent 27-inch Screen
    Sony VPL-HW50ES – Sony’s Best Home Cinema Projector
    Windows Vista : Installing and Running Applications - Launching Applications
    Most View
    Bamboo Splash - Powerful Specs And Friendly Interface
    Powered By Windows (Part 2) - Toshiba Satellite U840 Series, Philips E248C3 MODA Lightframe Monitor & HP Envy Spectre 14
    MSI X79A-GD65 8D - Power without the Cost
    Canon EOS M With Wonderful Touchscreen Interface (Part 1)
    Windows Server 2003 : Building an Active Directory Structure (part 1) - The First Domain
    Personalize Your iPhone Case
    Speed ​​up browsing with a faster DNS
    Using and Configuring Public Folder Sharing
    Extending the Real-Time Communications Functionality of Exchange Server 2007 : Installing OCS 2007 (part 1)
    Google, privacy & you (Part 1)
    iPhone Application Development : Making Multivalue Choices with Pickers - Understanding Pickers
    Microsoft Surface With Windows RT - Truly A Unique Tablet
    Network Configuration & Troubleshooting (Part 1)
    Panasonic Lumix GH3 – The Fastest Touchscreen-Camera (Part 2)
    Programming Microsoft SQL Server 2005 : FOR XML Commands (part 3) - OPENXML Enhancements in SQL Server 2005
    Exchange Server 2010 : Track Exchange Performance (part 2) - Test the Performance Limitations in a Lab
    Extra Network Hardware Round-Up (Part 2) - NAS Drives, Media Center Extenders & Games Consoles
    Windows Server 2003 : Planning a Host Name Resolution Strategy - Understanding Name Resolution Requirements
    Google’s Data Liberation Front (Part 2)
    Datacolor SpyderLensCal (Part 1)