One question that has been coming up frequently over the past few days has been how to launch the browser when the user clicks on a hyperlink. The answer is different for the browser and standalone cases:
Browser (XBAP or Loose XAML)
XBAPs and loose XAML support special named targets for Hyperlink; these are the same named targets that are supported by HTML:
- _self: the browser should load the document in the same frame as the element that refers to this target
- _parent: the browser should load the document into the immediate parent frame of the current frame. This value is equivalent to _self if the current frame has no parent.
- _blank: the browser should load the document in a new, unnamed window
- _top: the browser should load the document into the full, original window (thus canceling all other frames). This value is equivalent to _self if the current frame has no parent.
The following XAML illustrates using a special target:
<TextBlock> <Hyperlink NavigateUri="http://microsoft.com" TargetName="_top"> Navigate the top-level window to Microsoft.com </Hyperlink> </TextBlock>
If you specify a target that does not exist, a new browser window will be created and the navigation will occur in that window. If you specify a target that already exists, the existing browser window will be used.
This functionality all relies on the named browser targeting feature that already exists in Internet Explorer. Note that the WPF navigation framework also supports named targets, so if you have a Frame in your Window named “_self,” “_top,” etc., those Frames will be searched first before delegating to the browser. This means that the hyperlink targeting logic will start by looking for an Avalon Frame inside the application with the specified name. If it does not find it, and the application is hosted in the browser, then it will call the browser’s navigate function with the specified target name.
I have a sample HTML page that hosts loose XAML in an iframe and demonstrates each of the special hyperlink targets. You can try it out here, and download both pages from here.
Standalone
Unfortunately WPF v1 does not have this convenient feature in the standalone case as well, since we can’t call into the browser in this case. For standalone scenarios, the simplest way to accomplish this task is to handle Hyperlink’s RequestNavigate event, and launch the default browser in your event handler.
The following XAML creates the Hyperlink and attaches an event handler:
<TextBlock> <Hyperlink RequestNavigate="HandleRequestNavigate" Name="hl" NavigateUri="http://microsoft.com"> Open Microsoft.com in the default browser </Hyperlink> </TextBlock>
And the following code implements the event handler:
void HandleRequestNavigate(object sender, RoutedEventArgs e) { string navigateUri = hl.NavigateUri.ToString(); // if the URI somehow came from an untrusted source, make sure to // validate it before calling Process.Start(), e.g. check to see // the scheme is HTTP, etc. Process.Start(new ProcessStartInfo(navigateUri)); e.Handled = true; }
You can download the source for this sample here.
Forget the post i send a basic mistake on a isDocumentEnabled property took me time to realize i wans drunk… i mean wrong…:D
(just joking) i didn’t check the isDocumentEnabled at first.