SIGN UP MEMBER LOGIN:    
ARTICLE

How to Embed HTML File in Windows Phone 7

Posted by Raj Kumar Articles | Windows Phone in C# September 09, 2011
This article will explain how to embed HTML content and show in WebBrowser control in Silverlight Windows Phone 7.
Reader Level:
Download Files:
 

This article will explain how to embed HTML content and show in WebBrowser control in Silverlight Windows Phone 7.

Getting Started

Creating a Windows Phone Application:

ï‚·  Open Visual Studio 2010.

ï‚·  Go to File => New => Project

ï‚·  Select Silverlight for Windows Phone from the Installed templates and choose Windows Phone Application

ï‚·  Enter the Name and choose the location.


Click OK

First of all add a new .htm file in application and put some text in body tag like this:

<html>

<head>

<title>This page shows how to embed HTML Content in WP7</title>

</head>

<body>

 <h1>Welcome!</h1>

 <br />

 <h1>This is test HTML file</h1>

    <h1>To see the all articles by Raj Kumar on C-SharpCorner.com</h1>

    <h1>Click here "<a

            href="http://www.c-sharpcorner.com/Authors/raj1979/raj-kumar.aspx">Raj

        Kumar&#39;s World</a>&quot;</h1>

</body>

</html> 

Now drag and drop a WebBrowser control on page and a button and put this code on button click event. 

MainPage.xaml 

<Grid x:Name="LayoutRoot" Background="Transparent">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>

            <TextBlock x:Name="PageTitle" Text="Embed HTML" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

        </StackPanel>

        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <phone:WebBrowser HorizontalAlignment="Left" Margin="10,73,0,0" Name="webBrowser1" VerticalAlignment="Top" Width="440" Height="528" />

            <Button Content="Load HTML" Height="72" HorizontalAlignment="Left" Margin="-4,10,0,0" x:Name="btnLoadHTML" VerticalAlignment="Top" Width="200" FontSize="18.667" FontFamily="Andy" Click="btnLoadHTML_Click" >

                   <Button.Background>

                             <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                                      <GradientStop Color="Black" Offset="0"/>

                                      <GradientStop Color="#FFE44040" Offset="1"/>

                             </LinearGradientBrush>

                   </Button.Background>

            </Button>

        </Grid>

    </Grid>

 

MainPage.xaml.cs 

Here are the namespace you have to add first: 

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

First I am storing html file in Isolated Storage. 

What is Isolated Storage? 

Isolated Storage is like cookies which provide the ability to store data on client between application invocations but unlike cookies Isolated Storage is full-fledged virtual file system, it provides the application ability to Create, Read, Write, Delete and enumerate files and directories inside the virtual file system.

private void SaveHTMLFile()

        {

            string fileName = "TextFile1.htm";

            IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication();

            if (isolatedStorageFile.FileExists(fileName) == true)

            {

                isolatedStorageFile.DeleteFile(fileName);

            }

            StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));

            using (BinaryReader binaryReader = new BinaryReader(streamResourceInfo.Stream))

            {

                byte[] data = binaryReader.ReadBytes((int)streamResourceInfo.Stream.Length);

 

                using (BinaryWriter bw = new BinaryWriter(isolatedStorageFile.CreateFile(fileName)))

                {

                    bw.Write(data);

                    bw.Close();

                }

            }

        } 

  private void btnLoadHTML_Click(object sender, System.Windows.RoutedEventArgs e)

        {

              // TODO: Add event handler implementation here.

            SaveHTMLFile();

            webBrowser1.Navigate(new Uri("TextFile1.htm", UriKind.Relative));

        } 

Now time to run the application to see the result on button click.

 img1.jpg 

Image 1.

We are done here. Question or comments are most welcome. Drop me a comment in c-sharpcorner comments section.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor