Tap and Click Events
Since the release of the Windows Phone 7.1 SDK, the Click
event of the ButtonBase
class has been superseded by a UIElement
event named Tap
. The Tap
event is designed for touch input, while the Click
event relies on legacy code that handles mouse state.
Note
While the ButtonBase Click
event is still present in the SDK, unless your UI code needs to be shared with Silverlight for the browser projects, use the Tap
event.
The following is an example of a UIElement.Tap
event handler.
void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
...
}
Note
There is another public GestureEventArgs
class residing in the Windows Phone Toolkit that is, unfortunately,
located in the Microsoft.Phone.Controls namespace. If you are using the
Windows Phone Toolkit, be mindful that the correct type needs to be
specified or a runtime exception will be raised.
Either use the namespace qualified type name,
as shown in the previous example, or add a type alias to the top of
your class file, like so:
using GestureEventArgs = System.Windows.Input.GestureEventArgs;
Button Click Mode
If you need to use the Button.Click
event, rather than the Tap
event for whatever reason, you can tailor the type of user interaction that is required to raise the Button
’s Click
event using the Button.ClickMode
property.
The following is a list of the three possible ClickMode
values:
- Press—The Click
event is raised as soon as the user touches the button.
- Release—The Click
event is not raised until the user lifts his finger off the button. This is the default value.
- Hover—Has the same effect as Press
.
As there is currently no support for detecting when the user hovers
over a control on a Windows Phone device, this value serves no purpose
and exists to maintain compatibility with Silverlight for the browser.
Hyperlink Button
The HyperlinkButton
allows you to embed hypertext links in a page.
As previously stated, all UIElements
have a Tap
event, which can be used to perform navigation within an event handler.
Performing navigation in this manner, however, can be cumbersome, and
the HyperlinkButton
provides an easier way to navigate to pages within your app without having to subscribe to an event or use an ICommand
.
The HyperlinkButton
includes a NavigateUri
property. When a user presses the HyperlinkButton
, the button takes care of performing the navigation.
The default appearance of the HyperlinkButton
does not resemble a button at all. In fact, the default style of a HyperlinkButton
is underlined text, not unlike an HTML hyperlink.