Learn how to implement the tap, slide, swipe, pinch, and stretch touch interactions in a Windows Store business app. Data binding is used to connect standard Windows controls that use touch gestures to the view models that implement those gestures.

You will learn

Applies to

Windows Runtime for Windows 8.1

C#

Extensible Application Markup Language (XAML)

Making key decisions

Touch interactions in Windows use physical interactions to emulate the direct manipulation of UI elements and provide a more natural, real-world experience when interacting with those elements on the screen. The following list summarizes the decisions to make when implementing touch interactions in your app:

When displaying a list of items, do the touch targets for each item need to be identically sized?

Should you provide feedback to touch interactions?

Should touch interactions be reversible?

How long should a touch interaction last?

When should you use static gestures versus manipulation gestures?

Do you need to design and implement a custom interaction?

Does the custom interaction require specific hardware support such as a minimum number of touch points?

How will the custom interaction be provided on a non-touch device?

Windows provides a concise set of touch interactions that are used throughout the system. Applying this language consistently makes your app feel familiar to what users already know, increasing user confidence by making your app easier to learn and use. Most apps will not require touch interactions that are not part of the Windows touch language. For more info see Touch interaction design.

There are no definitive recommendations for how large a touch target should be or where it should be placed within your app. However, there are some guidelines that should be followed. The size and target area of an object depend on various factors, including the user experience scenarios and interaction context. They should be large enough to support direct manipulation and provide rich touch interaction data. It is acceptable in some user experience scenarios for touch targets in a collection of items to be different sizes. For instance, when displaying a collection of products you could choose to display some products at a larger size than the majority of the collection, in order to draw attention to specific products. Touch targets should react by changing color, changing size, or by moving. Non-moving elements should return to their default state when the user slides or lifts their finger off the element. In addition, touch interactions should be reversible. You can make your app safe to explore using touch by providing visual feedback to indicate what will happen when the user lifts their finger. For more info see Guidelines for targeting and Guidelines for visual feedback.

Touch interactions that require compound or custom gestures need to be performed within a certain amount of time. Try to avoid timed interactions like these because they can often be triggered accidentally and can be difficult to time correctly. For more info see Responding to user interaction.

Static gestures events are triggered after an interaction is complete and are used to handle single-finger interactions such as tapping. Manipulation gesture events indicate an ongoing interaction and are used for dynamic multi-touch interactions such as pinching and stretching, and interactions that use inertia and velocity data such as panning. This data is then used to determine the manipulation and perform the interaction. Manipulation gesture events start firing when the user touches the element and continue until the user lifts their finger or the manipulation is cancelled. For more info see Gestures, manipulations, and interactions.

Only create a custom interaction and if there is a clear, well-defined requirement and no interaction from the Windows touch language can support your scenario. If an existing interaction provides the experience your app requires, adapt your app to support that interaction. If you do need to design and implement a custom interaction you will need to consider your interaction experience. If the interaction depends on items such as the number of touch points, velocity, and inertia, ensure that these constraints and dependencies are consistent and discoverable. For example, how users interpret speed can directly affect the functionality of your app and the users satisfaction with the experience. In addition, you will also have to design and implement an equivalent version of the interaction for non-touch devices. For more info see Responding to user interaction.

Important To avoid confusing users, do not create custom interactions that duplicate or redefine existing, standard interactions.

Touch in AdventureWorks Shopper

As previously described in Designing the UX, touch is more than simply an alternative to using a mouse. We wanted to make touch an integrated part of the app because touch can add a personal connection between the user and the app. Touch is also a natural way to enable users to browse and select products. In addition, we use Semantic Zoom to highlight how levels of related complexity can easily be navigated. With Semantic Zoom users can easily visualize high level content such as categories, and then zoom into those categories to view category items.

The AdventureWorks Shopper reference implementation uses the Windows touch language. We use the standard touch interactions that Windows provides for these reasons:

The Windows Runtime provides an easy way to work with them.

We don't want to confuse users by creating custom interactions.

We want users to use the interactions that they already know to explore the app, and not need to learn new interactions.

We also wanted AdventureWorks Shopper to be intuitive for users who use a mouse or similar pointing device. The built-in controls work as well with a mouse or other pointing device as they do with touch. So when you design for touch, you also get mouse and pen functionality. For example, you can use the left mouse button to invoke commands. In addition, mouse and keyboard equivalents are provided for many commands. For example, you can use the right mouse button to activate the app bar, and holding the Ctrl key down while scrolling the mouse wheel controls Semantic Zoom interaction. For more info see Guidelines for common user interactions.

The document Touch interaction design explains the Windows touch language. The following sections describe how we applied the Windows touch language in AdventureWorks Shopper.

Tap for primary action

Tapping an element invokes its primary action. For example, on the GroupDetailPage, you tap on a product to navigate to the ItemDetailPage. The following diagram shows an example of the tap for primary action gesture in the AdventureWorks Shopper reference implementation.

Products are displayed on the GroupDetailPage in the AutoRotatingGridView custom control. This control displays a collection of items in a view state aware grid. This control is an ItemsControl, so it can contain a collection of items of any type. A benefit of using this control is that it derives from the GridView control that has touch capabilities built in.

To populate the AutoRotatingGridView custom control you can add objects directly to its Items collection or bind its ItemsSource property to a collection of data items. When you add items to a GridView-derived control they are automatically placed in a GridViewItem container that can be styled to change how an item is displayed.

The ItemsSource property specifies that the AutoRotatingGridView will bind to the Items property of the GroupDetailPageViewModel class. The Items property is initialized to a collection of type ProductViewModel when the GroupDetailPage is navigated to.

The appearance of individual items in the AutoRotatingGridView is defined by the ItemTemplate property. A DataTemplate is assigned to the ItemTemplate property that specifies that each item in the AutoRotatingGridView will display the product subtitle, image, and description.

Slide to pan

The slide gesture is primarily used for panning interactions. Panning is a technique for navigating short distances over small sets of content within a single view. Panning is only necessary when the amount of content in the view causes the content area to overflow the viewable area. For more info see Guidelines for panning. One of the uses of the slide gesture in the AdventureWorks Shopper reference implementation is to pan among products in a category. For example, when you browse to a product, you can use the slide gesture to navigate to the previous or next product in the subcategory. The following diagram shows an example of the slide to pan gesture in AdventureWorks Shopper.

In AdventureWorks Shopper this gesture is implemented by the FlipView control. The FlipView control displays a collection of items, and lets you flip through them one at a time. The FlipView control is derived from the ItemsControl class, like the GridView control, and so it shares many of the same features. A benefit of using the FlipView control is that it has touch capabilities built in, removing the need for additional code.

To populate a FlipView you can add objects directly to its Items collection or bind its ItemsSource property to a collection of data items. When you add items to a FlipView they are automatically placed in a FlipViewItem container that can be styled to change how an item is displayed.

Swipe to select, command, and move

With the swipe gesture, you slide your finger perpendicular to the panning direction to select objects. The ability to use the swipe gesture depends upon the value of the SelectionMode property on the ListViewBase-derived control. A value of None indicates that item selection is disabled, while a value of Single indicates that single items can be selected using this gesture.

In the AdventureWorks Shopper reference implementation, the swipe gesture can be used to select items on the ChangeDefaultsFlyout, the CheckoutSummaryPage, and the ShoppingCartPage. When an item is selected on the ShoppingCartPage the bottom app bar appears with the app bar commands applying to the selected item. The following diagram shows an example of the swipe to select, command, and move gesture in AdventureWorks Shopper.

The IsSwipeEnabled property of the GridView control indicates whether a swipe gesture is enabled for the control. Setting IsSwipeEnabled to false disables some default touch interactions, so it should be set to true when these interactions are required. For example, when IsSwipeEnabled is false:

If item selection is enabled, a user can deselect items by right-clicking with the mouse, but cannot deselect an item with touch by using the swipe gesture.

If CanDragItems is true, a user can drag items with the mouse, but not with touch.

If CanReorderItems is true, a user can reorder items with the mouse, but not with touch.

The AdventureWorks Shopper reference implementation does not explicitly set the IsSwipeEnabled property, as its default value is true. The following code example shows how an item on the ShoppingCartPage can be selected with the swipe gesture.

The SelectedItem property of the AutoRotatingGridView custom control can be used to retrieve the item selected by the swipe gesture. Here the SelectedItem property performs a two-way binding to the SelectedItem property of the ShoppingCartPageViewModel class, which is shown in the following code example.

Pinch and stretch to zoom

Pinch and stretch gestures are not just for magnification, or performing optical zoom. The AdventureWorks Shopper reference implementation uses Semantic Zoom to help users navigate between large sets of data. Semantic Zoom enables you to switch between two different views of the same content. You typically have a main view of your content and a second view that allows users to quickly navigate through it. Users can pan or scroll through categories of content, and then zoom into those categories to view detailed information. The following diagram shows an example of the pinch and stretch to zoom gesture in AdventureWorks Shopper.

To provide this zooming functionality, the SemanticZoom control uses two other controls—one to provide the zoomed-in view and one to provide the zoomed-out view. These controls can be any two controls that implement the ISemanticZoomInformation interface. XAML provides the ListView and GridView controls that meet this criteria.

For the zoomed-in view, we display a MultipleSizedGridView custom control that binds to products that are grouped by sub-category. The MultipleSizedGridView also shows a title (the category) for each group.

The ItemsSource property specifies the items to be displayed by the MultipleSizedGridView. The groupedItemsViewSource static resource is a CollectionViewSource that provides the source data for the control.

The RootCategories property on the HubPageViewModel specifies the data that is bound to the MultipleSizedGridView for the zoomed-in view. RootCategories is a collection of CategoryViewModel objects. The ItemsPath property refers to the Products property of the CategoryViewModel class. Therefore, the MultipleSizedGridView will show each product grouped by the category it belongs to.

For the zoomed-out view, we display a GridView that binds to filled rectangles for each category. Within each category the category title and number of products is displayed.

Swipe from edge for app commands

When there are relevant commands to display, the Adventure Works Shopper reference implementation displays the app bar when the user swipes from the bottom or top edge of the screen. Every page can define a navigation bar, a bottom app bar, or both. For instance, AdventureWorks Shopper displays both when you activate the app bars on the ShoppingCartPage. The following diagram shows an example of the swipe from edge for app commands gesture in AdventureWorks Shopper.

The AppBar and CommandBar controls are toolbars for displaying app-specific commands. AdventureWorks Shopper displays app bars on each page. The Page.TopAppBar property can be used to define the navigation bar, with the Page.BottomAppBar property being used to define the bottom app bar. Each of these properties will contain either an AppBar or CommandBar control that holds the app bar's UI components. In general, you should use the bottom app bar for contextual commands that act on the currently selected item on the page. Use the navigation bar for navigational elements that move the user to a different page.

AdventureWorks Shopper uses both AppBar and CommandBar controls. Bottom app bars are implemented by the CommandBar control, using AppBarButtons that define the commands that will appear on the app bar. A CommandBar must use AppBarButtons to display commands, and the default appearance of an AppBarButton is circular. Using a CommandBar control ensures that the commands will be laid out automatically, and resized when the app size changes.

AdventureWorks Shopper implements the navigation bar for each page as a user control named TopAppBarUserControl. This user control defines the Button controls that will appear in the navigation bar. Buttons are used to easily display non-circular commands, and must be placed inside an AppBar control.

When an item on a page is selected, the app bar is shown in order to display contextual commands, by setting the IsOpen property on the CommandBar control. If you have contextual commands on an app bar, the mode should be set to sticky while the context exists so that the bar doesn't automatically hide when the user interacts with the app. When the context is no longer present, sticky mode can be turned off. This can be achieved by setting the IsSticky property on the CommandBar control.

Swipe from edge for system commands

Users can swipe from the edge of the screen to reveal app bars and charms, or to display previously used apps. Therefore, it is important to maintain a sufficient distance between app controls and the screen edges. The following diagram shows an example of the swipe from edge for system commands gesture in AdventureWorks Shopper.