Simplest Web Browser For Windows Store

Introduction

In this article, I will show you how to create the simplest Metro style web browser. I will also show how to use the WebView Control.

This web browser contains the following functions:

  1. URL text box
  2. GO button
  3. Previous and Next Button
  4. Home Button
  5. Refresh Button

Now I will show you step-by-step how to create your own web browser for Windows Store app.

Procedure:

  1. Open Visual Studio.
  2. Go to the menu bar and select "File" -> "New" -> "Project..." or simply press "Ctrl+Shift+N".

     New Project

  3. You will see a new window; select "Blank App" in the Visual C# templates and select "Blank app (XAML)".
  4. Now provide the name of the application and location name.
  5. Now go to the Solution Explorer and open MainPage.xaml Page and now start to make a webbrowser.
  6. For creating a web browser you need to use one main Control called Web View Control.
  7. Drag and drop one Web View control on the XAML page and increase or write following code for that:
    1. <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">  
    2.         <WebView x:Name="WVWebBrowser" HorizontalAlignment="Left" Height="687" Margin="10,71,0,0" VerticalAlignment="Top" Width="1346"/>  
    3.           
    4.     </Grid>  
  8. Now Open to the MainPage.xaml.cs and inside the MainPage() constructor write the following code:
    1. public MainPage()  
    2.         {  
    3.             this.InitializeComponent();  
    4.             WVWebBrowser.Navigate(new Uri("http://www.google.com"));  
    5.         }  
    Here WVWebBrowser is an object of the WebView Control Class, and Navigate is the Method that Written Type is Void and use arguments as an object of the Uri Class. So by using the "new" keyword we are making the object of the Uri Class and call the parameterized constructor that takes a string in the URL Format. So I have written "http://www.google.com".
  9. After this if we build the project and run it then on the load time the WebView Object will be navigated to the Google page that is "http://www.google.com" because at load time I am calling the Navigate Method of the WebView class. So the output will be like:

    google

  10. Now I am stopping the running project and adding one TextBox and Button named "Go".  In the TextBox the user will provide the Uri and when he clicks on the Go button he will be navigate to the new page depending on the Text Box Uri.
    So drag and drop one TextBox and one Button or use the following XAML code for these controls:
    1. <!--TEXT BOX For The Uri-->  
    2. <TextBox Name="tbURL" Text="http://www.google.com"  HorizontalAlignment="Left" Margin="77,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="588"/>  
    3.   
    4. <!--GO Button-->  
    5.         <Button Name="btnGo"  Content="GO" HorizontalAlignment="Left" Margin="670,10,0,0" VerticalAlignment="Top"/> 
  11. After writing the code above double-click on the GO button and fire it's click event, and for the click event of the Go button write the following code:
    1. //GO Button Click Event Code...  
    2.         private void btnGo_Click(object sender, RoutedEventArgs e)  
    3.         {  
    4.             Uri uri = new Uri("http://" + tbURL.Text);  
    5.             try  
    6.             {  
    7.                 WVWebBrowser.Navigate(uri);  
    8.             }  
    9.             catch (Exception ex)  
    10.             {  
    11.                 tbURL.Text = "invalid adress !";  
    12.             }  
    13.         }  
  12. Now after writing the code above build again and run the application, enter the URL in the text box and click on the GO button. Suppose I write c-sharpcorner in the text box and I click on GO button then:

    c-sharpcorner

    After clicking on the GO button the result will be:

    Csharpcorner

  13. Now in the next step I am creating 3 buttons that are:

    HOME (For redirect to Home Page)
    BACK (For going to the Previous Page)
    NEXT (For going to the Next Page)
    Refresh (For Reload again)

  14. So for creating these buttons write the following XAML code.
    1. <!--Back Button-->  
    2.        <Button Name="btnBack" Content="BACK" HorizontalAlignment="Left" Margin="10,9,0,0" VerticalAlignment="Top" Width="72" />  
    3.   
    4. <!--Next Button-->  
    5.         <Button Name="btnNext" Content="NEXT" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="87,10,0,0" Width="72" />  
    6.   
    7. <!--Home Button-->  
    8.         <Button Name="btnHome" Content="HOME" HorizontalAlignment="Left" Margin="815,10,0,0" VerticalAlignment="Top" />  
    9.   
    10. <!--Refresh Button-->  
    11.         <Button Name="btnRefresh" Content="Refresh" HorizontalAlignment="Left" Margin="893,10,0,0" VerticalAlignment="Top"/> 
  15. Now I am creating a list of string types at the class level in which we can save all the states or we can say URLs for saving the next page and the previous page as in the following:
    1. List<string> sitesList = new List<string>();  
  16. Now for the click event of the back button write the following sample code:
    1. private void btnBack_Click(object sender, RoutedEventArgs e)  
    2.         {  
    3.             try  
    4.             {  
    5.                 int i = List_Of_Sites.IndexOf(tbURL.Text) - 1;  
    6.                 if (i < 0)  
    7.                 {  
    8.                     tbURL.Text = List_Of_Sites[List_Of_Sites.Count - 1];  
    9.                 }  
    10.                 else  
    11.                 {  
    12.                     tbURL.Text = List_Of_Sites[i];  
    13.                 }  
    14.   
    15.                 WVWebBrowser.Navigate(new Uri(tbURL.Text));  
    16.                 save = tbURL.Text;  
    17.             }  
    18.             catch (Exception exc) { }  
    19.   
    20.         } 
  17. Code for the NEXT button's click event:
    1. private void btnNext_Click(object sender, RoutedEventArgs e)  
    2.         {  
    3.             try  
    4.             {  
    5.                 int i = List_Of_Sites.IndexOf(tbURL.Text) - 1;  
    6.                 if (i < 0)  
    7.                 {  
    8.                     tbURL.Text = List_Of_Sites[List_Of_Sites.Count - 1];  
    9.                 }  
    10.                 else  
    11.                 {  
    12.                     tbURL.Text = List_Of_Sites[i];  
    13.                 }  
    14.   
    15.                 WVWebBrowser.Navigate(new Uri(tbURL.Text));  
    16.             }  
    17.             catch (Exception exc) { }  
    18.         } 
  18. Code for home button's click events:
    1. private void btnHome_Click(object sender, RoutedEventArgs e)  
    2.         {  
    3.             WVWebBrowser.Navigate(new Uri("http://www.google.com"));  
    4.   
    5.             List_Of_Sites.Add(tbURL.Text);  
    6.         }  
  19. Code for the Refresh button's click event:
    1. private void btnRefresh_Click(object sender, RoutedEventArgs e)  
    2. {  
    3.     WVWebBrowser.Navigate(new Uri("http://"+save));  
    4. }  
Complete Code
  • MainPage.xaml code:
    1. <Page  
    2.     x:Class="S_WebBrowser.MainPage"  
    3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    5.     xmlns:local="using:S_WebBrowser"  
    6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
    7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
    8.     mc:Ignorable="d">  
    9.   
    10.     <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">  
    11.   
    12.         <WebView x:Name="WVWebBrowser" HorizontalAlignment="Left" Height="711" Margin="10,47,0,0" VerticalAlignment="Top" Width="1346"/>  
    13.   
    14.         <TextBox Name="tbURL" Text="http://www.google.com"  HorizontalAlignment="Left" Margin="164,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="588"/>  
    15.   
    16.         <Button Name="btnGo"  Content="GO" HorizontalAlignment="Left" Margin="759,10,0,0" VerticalAlignment="Top" Click="btnGo_Click"/>  
    17.   
    18.         <Button Name="btnBack" Content="BACK" HorizontalAlignment="Left" Margin="10,9,0,0" VerticalAlignment="Top" Width="72" Click="btnBack_Click"/>  
    19.   
    20.         <Button Name="btnNext" Content="NEXT" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="87,10,0,0" Width="72" Click="btnNext_Click"/>  
    21.   
    22.         <Button Name="btnHome" Content="HOME" HorizontalAlignment="Left" Margin="815,10,0,0" VerticalAlignment="Top" Click="btnHome_Click"/>  
    23.   
    24.         <Button Name="btnRefresh" Content="Refresh" HorizontalAlignment="Left" Margin="893,10,0,0" VerticalAlignment="Top" Click="btnRefresh_Click"/>  
    25.   
    26.     </Grid>  
    27. </Page>  
  •  MainPage.xaml.cs page code:
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.IO;  
    4. using System.Linq;  
    5. using Windows.Foundation;  
    6. using Windows.Foundation.Collections;  
    7. using Windows.UI.Xaml;  
    8. using Windows.UI.Xaml.Controls;  
    9. using Windows.UI.Xaml.Controls.Primitives;  
    10. using Windows.UI.Xaml.Data;  
    11. using Windows.UI.Xaml.Input;  
    12. using Windows.UI.Xaml.Media;  
    13. using Windows.UI.Xaml.Navigation;  
    14.   
    15. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238  
    16.   
    17. namespace S_WebBrowser  
    18. {  
    19.     public sealed partial class MainPage : Page  
    20.     {  
    21.         //List which will store history  
    22.         List<string> List_Of_Sites = new List<string>();  
    23.         //save Present URL  
    24.         string save;  
    25.         public MainPage()  
    26.         {  
    27.             this.InitializeComponent();  
    28.             WVWebBrowser.Navigate(new Uri("http://www.google.com"));//Default Webpage when user will click   
    29.             List_Of_Sites.Add(tbURL.Text);//First time add the URL to the list  
    30.             save = "www.google.com";  
    31.         }  
    32.         protected override void OnNavigatedTo(NavigationEventArgs e)  
    33.         {  
    34.         }  
    35.         //GO Button Click Event Code  
    36.         private void btnGo_Click(object sender, RoutedEventArgs e)  
    37.         {  
    38.             Uri uri = new Uri("http://" + tbURL.Text);  
    39.             try  
    40.             {  
    41.                 WVWebBrowser.Navigate(uri);  
    42.                 List_Of_Sites.Add(tbURL.Text);  
    43.                 save = tbURL.Text;  
    44.             }  
    45.             catch (Exception ex)  
    46.             {  
    47.                 tbURL.Text = "invalid adress !";  
    48.             }  
    49.         }  
    50.   
    51.         //Back Button Code  
    52.         private void btnBack_Click(object sender, RoutedEventArgs e)  
    53.         {  
    54.             try  
    55.             {  
    56.                 int i = List_Of_Sites.IndexOf(tbURL.Text) - 1;  
    57.                 if (i < 0)  
    58.                 {  
    59.                     tbURL.Text = List_Of_Sites[List_Of_Sites.Count - 1];  
    60.                 }  
    61.                 else  
    62.                 {  
    63.                     tbURL.Text = List_Of_Sites[i];  
    64.                 }  
    65.   
    66.                 WVWebBrowser.Navigate(new Uri(tbURL.Text));  
    67.                 save = tbURL.Text;  
    68.             }  
    69.             catch (Exception exc) { }  
    70.   
    71.         }  
    72.   
    73.         //Next Button Code  
    74.         private void btnNext_Click(object sender, RoutedEventArgs e)  
    75.         {  
    76.             try  
    77.             {  
    78.                 int i = List_Of_Sites.IndexOf(tbURL.Text) - 1;  
    79.                 if (i < 0)  
    80.                 {  
    81.                     tbURL.Text = List_Of_Sites[List_Of_Sites.Count - 1];  
    82.                 }  
    83.                 else  
    84.                 {  
    85.                     tbURL.Text = List_Of_Sites[i];  
    86.                 }  
    87.   
    88.                 WVWebBrowser.Navigate(new Uri(tbURL.Text));  
    89.             }  
    90.             catch (Exception exc) { }  
    91.         }  
    92.   
    93.         //Home Button Code  
    94.         private void btnHome_Click(object sender, RoutedEventArgs e)  
    95.         {  
    96.             WVWebBrowser.Navigate(new Uri("http://www.google.com"));  
    97.   
    98.             List_Of_Sites.Add(tbURL.Text);  
    99.         }  
    100.   
    101.         //Refresh Button code  
    102.         private void btnRefresh_Click(object sender, RoutedEventArgs e)  
    103.         {  
    104.             WVWebBrowser.Navigate(new Uri("http://"+save));  
    105.         }  
    106.     }  
    107. }  
Now after completion build this web browser and run it; you will get output such as:

output


Similar Articles