Universal Windows Platform – Web Browser

Web Browser demonstrates how to use the WebView control to display and navigate through websites

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.

vs2017

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
vs2017-new-project-window

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.

vs2017-target-platform

The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…

vs2017-project-add-new-item

Step 6

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project

vs2017-add-new-item-library

Step 7

Once in the Code View for Library.cs the following should be entered:

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

public class Library
{
    public void Back(ref WebView web)
    {
        if (web.CanGoBack)
        {
            web.GoBack();
        }
    }

    public void Forward(ref WebView web)
    {
        if (web.CanGoForward)
        {
            web.GoForward();
        }
    }

    public void Go(ref WebView web, string value, KeyRoutedEventArgs args)
    {
        if (args.Key == Windows.System.VirtualKey.Enter)
        {
            try
            {
                web.Navigate(new Uri(value));
            }
            catch
            {

            }
            web.Focus(FocusState.Keyboard);
        }
    }
}

In the Library.cs there are using statements to include the necessary functionality. The Back method checks if it’s possible to Go back through visited websites in the control and if so it does so, the same can be said of the Forward method but checks and goes forward through visited websites instead. The Go method is triggered each time a keystroke is make and once this is an Enter keystroke then it will Navigate the WebView to the Uri determined by the string values passed in.

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage

Step 9

From the Menu choose View and then Designer

vs2017-view-designer

Step 10

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<Grid>
	<Grid.RowDefinitions>
		<RowDefinition Height="Auto"/>
		<RowDefinition Height="*"/>
	</Grid.RowDefinitions>
	<TextBox Grid.Row="0" Margin="20" Name="Value" VerticalAlignment="Center" InputScope="Url" KeyDown="Go_KeyDown"/>
	<WebView Grid.Row="1" Margin="20" Name="Display" NavigationCompleted="Display_NavigationCompleted"/>
</Grid>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="Back" Icon="Back" Label="Back" Click="Back_Click"/>
	<AppBarButton Name="Forward" Icon="Forward" Label="Forward" Click="Forward_Click"/>
	<AppBarButton Icon="Refresh" Label="Refresh" Click="Refresh_Click"/>
	<AppBarButton Icon="Cancel" Label="Stop" Click="Stop_Click"/>
</CommandBar>

The first block of XAML the main user interface of the Application, this features a Grid with a top row to display a TextBox which is where the website address can be typed in, followed by Enter to navigate and then there’s the WebView itself in the second row. The second block of XAML is is the CommandBar which contains the Back, Forward, Refresh and Stop commands to control the WebView.

Step 11

From the Menu choose View and then Code

vs2017-view-code

Step 12

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

Library library = new Library();

private void Back_Click(object sender, RoutedEventArgs e)
{
	library.Back(ref Display);
}

private void Forward_Click(object sender, RoutedEventArgs e)
{
	library.Forward(ref Display);
}

private void Refresh_Click(object sender, RoutedEventArgs e)
{
	Display.Refresh();
}

private void Stop_Click(object sender, RoutedEventArgs e)
{
	Display.Stop();
}

private void Go_KeyDown(object sender, KeyRoutedEventArgs e)
{
	library.Go(ref Display, Value.Text, e);
}

private void Display_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
	if (args.IsSuccess)
	{
		Value.Text = args.Uri.ToString();
	}
}

Below the MainPage() Method an instance of the Library Class is created, then in the Back_Click and Forward_Click methods the methods from the Library class are called for those functions. In the Refresh_Click and Stop_Click methods those functions of the WebView are called. In the Go_KeyDown this passes through the text of the TextBox along with additional event information for the keystrokes, and finally the Display_NavigationCompleted this will on that Event populate the TextBox with the current website page address

Step 13

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-local-machine

Step 14

After the Application has started running you can then type in the URL of any website then press or tap enter to load it. You can go Back and Forward through any websites loaded plus Refresh to reload a displayed website or Stop to stop one from loading.

ran-web-browser

Step 15

To Exit the Application select the Close button in the top right of the Application

vs2017-close

Creative Commons License

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s