Navigating to a Web Page Using the WebBrowserTask

,

The web browser task is used to launch the built-in Web Browser app and optionally navigates the browser to a specified URL:

Uri uri = new Uri("http://msdn.microsoft.com", UriKind.RelativeOrAbsolute);
WebBrowserTask task = new WebBrowserTask { Uri = uri };
task.Show();

If the Uri results in a 404 HTTP standard response code (Not Found), no exception is raised; instead the Windows Phone Search application is launched.

Sample Overview

Example code for the WebBrowserTask can be found in the LaunchWebBrowserViewModel in the downloadable sample code.

The LaunchWebBrowserView page contains a TextBox in which the user can enter a URL and a Launch Web Browser Button (see Figure 14.23).

Image

FIGURE 14.23 Web Browser Launcher page.

The view’s TextBox has a TwoWay data binding to the viewmodel’s Url property, as shown:

<TextBox Text="{Binding Url, Mode=TwoWay}" InputScope="Url" />

The InputScope property of the TextBox causes the onscreen keyboard to be displayed with a set of keys suitable for typing a URL. For more information on InputScopes, see Chapter 6.

A button is bound to an ICommand in the viewmodel named LaunchCommand. The command is instantiated in the viewmodel’s constructor. When executed, LaunchCommand creates a new WebBrowserTask and sets its Uri property using the text supplied by a TextBox in the view. See the following excerpt:

public LaunchWebBrowserViewModel()
{
    launchCommand = new DelegateCommand(
        delegate
            {
                WebBrowserTask task = new WebBrowserTask { URL = url };
                task.Show();
            });
}

When the WebBrowserTask.Show method is called, the built-in Web Browser app is launched.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.226.28