Creating A Mini Browser Using Visual Studio

In this article, we will be creating a mini browser using Visual Studio.
 
STEP 1

In the "New Project" window, expand the installed Visual C# or Visual Basic templates and then select the Windows Phone templates. At the bottom of the New Project window, type MiniBrowser as the project’s Name.
 
STEP 2

Click OK. In the "New Windows Phone Application" dialog box, select Windows Phone OS 8.0 as the target Windows Phone OS Version.
 
STEP 3

Click OK. The new project is created and opens in Visual Studio. The designer displays MainPage.xaml which contains the user interface for your app.
STEP 4

The next step is to lay out the controls that make up the UI of the app, using the Visual Studio Designer. After you add the controls, the final layout will look somewhat similar to the following screenshot.
 
To create the UI -
  1. Open the Properties window in Visual Studio (if it’s not already open), by selecting the VIEW | Properties Window menu command. The Properties window opens in the lower right corner of the Visual Studio window.

  2. Change the app title.

    1. In the Visual Studio Designer, click to select the MY APPLICATION TEXT BLOCK control. The properties of the control are displayed in the Properties window.
    2. In the TEXT property, change the name to MY FIRST APPLICATION to rename the app window title. If the properties are grouped by category in the Properties window, you can find TEXT in the Common category.
  1. Change the name of the page.

    1. In the Designer, click to select the page name TEXT BLOCK control.
    2. Change the property to Mini Browser to rename the app’s main page.

  2. Change the supported orientations.

    1. In the XAML code window, click the first line of the XAML code. The properties of the PHONE APPLICATION are displayed in the Properties window.
    2. Change the SUPPORTED ORIENTATION property to PORTRAIT landscape to add support for both portrait and landscape orientations. If the properties are grouped by category in the Properties window, you can fin in the Common category.

  3. Open the Toolbox in Visual Studio, if it’s not already open, by selecting the VIEW | Toolbox menu command. The Toolbox typically opens on the left side of the Visual Studio window and displays the list of available controls for building the user interface. By default, the Toolbox is collapsed when you’re not using it.

  4. Add a textbox for the URL.

    1. From the Common Windows Phone Controls group in the Toolbox, add TEXT CONTROL control to the Designer surface by dragging it from the Toolbox and dropping it onto the Designer surface. Place the TEXT BOX just below the Mini Browser text. Use the mouse to size the control to the approximate width shown in the layout image above. Leave room on the right for the "Go" button.
    2. In the Properties window, set the following properties for the new text box.
      1. Add Button.
      2. Add the WebBrowser control.
C# coding
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Navigation;  
  8. using Microsoft.Phone.Controls;  
  9. using Microsoft.Phone.Shell;  
  10. using MiniBrowser.Resources;  
  11. namespace MiniBrowser {  
  12.     public partial class MainPage: PhoneApplicationPage {  
  13.         // Constructor  
  14.         public MainPage() {  
  15.             InitializeComponent();  
  16.         }  
  17.         private void Go_Click(object sender, RoutedEventArgs e) {}  
  18.     }  
  19. }  

When you double-click the "Go" button, Visual Studio also updates the XAML in MainPage.xaml to connect the button’s Click event to the Go_Click event handler.

XAML

  1. <Button x:Name="Go" Content="Go" HorizontalAlignment="Right" Margin="346,10,0,0" VerticalAlignment="Top" Click="Go_Click"/>  
In MainPage.xaml.cs or MainPage.xaml.vb, add the highlighted lines of code to the emptyGo_Click event handler. This code takes the URL that the user enters in the text box and navigates to that URL in the MiniBrowser control,
  1. private void Go_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     string site = URL.Text;  
  4.     MiniBrowser.Navigate(new Uri(site, UriKind.Absolute));  
  5. }  

Run your app

Now, you’ve finished your first Windows Phone app! Next, let's build, run, and debug the app.

Before you test the app, make sure that your computer has Internet access to be able to test the web browser control.

To run your app in the emulator -

  • Build the solution by selecting the BUILD | Build Solution menu command. If any error occurs, it's listed in the Error List window. You can open the Error List window, if it’s not already open, by selecting the VIEW | Error List menu command.

  • On the Standard toolbar, make sure the deployment target for the app is set to one of the values for the Windows Phone Emulator; for example, Emulator WVGA 512MB.
  • Run the app by pressing F5 or by selecting the DEBUG | Start Debugging menu command. This opens the emulator window and launches the app. If this is the first time you’re starting the emulator, it might take a few seconds for it to start and launch your app.
And, we are done. I hope you liked this article and found it informative.


Similar Articles