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.
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).
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.
3.15.226.28