TouchManager is a tool we've been using internally for a while. Now we're opening up this touch infrastructure up to the public so that you can use it too. This is a quick overview of the Telerik® UI for WPF and UI for Silverlight TouchManager.

Touch is such a huge part of our everyday life now. One can't even imagine an application not running well on a tablet. But in order for an application to be smooth in such an environment, it needs solid touch support. Translating touch events to mouse events and relying only on the previously existing mouse code is not enough. The application requires specific logic and the controls it uses need to be touch aware.

Our WPF controls have been touch aware for a long time now, but it recently occurred to us that our customers may also benefit if they could directly use the same touch infrastructure that we use. And so, I am happy to introduce the TouchManager!

Hello, Touch World!

Simply put, the TouchManager is a static class that makes it easy to attach event handlers to touch gestures such as Tap, Swipe, Pinch, etc. The API is very intuitive—if you want to start listening for Swipe and Pinch, you just need to use the AddSwipeEventHandler method and AddPinchEventHandler method respectively. You can place your code in the event handlers and let the TouchManager decide whether the user is swiping or pinching. When you handle a gesture, say Swipe, make sure to also handle the SwipeStarted and SwipeFinished events to avoid unexpected behavior with nested elements (more details here).

  TouchManager.AddSwipeStartedEventHandler(element, element_SwipeStarted);
  TouchManager.AddSwipeEventHandler(element, element_Swipe);
  TouchManager.AddSwipeFinishedEventHandler(element, element_SwipeFinished);

  void element_SwipeStarted(object sender, TouchEventArgs args)
  {
    // initialize some resources

    args.Handled = true;
  }

  void element_Swipe(object sender, TouchEventArgs args)
  {
    // execute swipe

    args.Handled = true;
  }

  void element_SwipeFinished(object sender, TouchEventArgs args)
  {
    // release resources

    args.Handled = true;
  }

You can also attach handlers for the basic events—TouchEnter, TouchDown, TouchMove, TouchUp and TouchLeave. The TouchManager uses these to recognize certain gestures. There are some predefined gestures—tap, tap-hold, tap-hold-release, swipe, pinch, and drag. A part of the TouchManager, namely the GestureManager class, makes it possible for you to implement custom gestures, so if you need a two-finger-swipe gesture, you can easily implement it and plug it into your application.

The TouchManager is actually not a brand new thing. Many of our WPF controls already used it internally, but it is now that we decided to make it public—we realized that if we find it so useful, then you may also find it useful. We have introduced a couple of new properties which can help if you have a ScrollViewer and touch input does not work for elements inside it. The reason the elements won't react well to touch input is that the ScrollViewer captures the touch and handles it, making it the only element receiving touch events. Usually this locking of the touch is due to the PanningMode. If you have access to the ScrollViewer you can use the ScrollViewerSwipeMode property of the TouchManager and set it to Self. If you don't have a convenient way to target the ScrollViewer, you can use a combination of the TouchMode and ScrollViewerSwipeMode properties.

<ListBox.ItemTemplate>
 <DataTemplate>
  <telerik:RadCartesianChart telerik:TouchManager.TouchMode="Locked" telerik:TouchManager.ScrollViewerSwipeMode="Parent" />

You can check out a live QSF example that solely uses the TouchManager and you can read more about it in the online documentation. You can learn more about other new updates coming as part of our latest Q1 2016 release, and let us know what you think!
Petar Marchev
About the Author

Petar Marchev

Petar Marchev is a developer in the Telerik XAML Team. Petar has a passion for desktop applications development, code optimizations and multithreaded programming. He is an audiophile and enjoys music from his full range speakers and tube amp.

Related Posts

Comments

Comments are disabled in preview mode.