Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a tap moveable control in WinRT?

I wrote a UserControl in WinRT and I want to make it moveable with a finger.
When I move it using a pen or mouse it is still moving but not when i use a finger.
The PointerMoved is not triggert when I use a finger.

Here is the simple xaml:

<UserControl>
    <Rectangle PointerPressed="PointerPressed" PointerMoved="PointerMoved"/>
</UserControl>

and here is the code:

private Point position;

void PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
    Rectangle r = sender as Rectangle;
    var pointerPoint = e.GetCurrentPoint(r);
    position = pointerPoint.Position;
}

void PointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{    
    Rectangle r = sender as Rectangle;   
    var delta = e.GetCurrentPoint(r).Position;
    r.Margin = new Thickness(r.Margin.Left + delta.X - position.X, r.Margin.Top + delta.Y - position.Y, 0, 0);
}

What do I miss here?

Edit:
I am working with Windows 8.1 and VisualStudio 2013.
Maybe it's a new feature^^

like image 761
Olaf Japp Avatar asked Feb 15 '23 20:02

Olaf Japp


1 Answers

It's simpler than you think!

<Rectangle Width="100" Height="100" Fill="White"
    ManipulationMode="TranslateX,TranslateY" 
    ManipulationDelta="Rectangle_ManipulationDelta_1" />

private void Rectangle_ManipulationDelta_1(object sender, ManipulationDeltaRoutedEventArgs e)
{
    var _Rectangle = sender as Windows.UI.Xaml.Shapes.Rectangle;
    var _Transform = (_Rectangle.RenderTransform as CompositeTransform)
        ?? (_Rectangle.RenderTransform = new CompositeTransform()) as CompositeTransform;
    _Transform.TranslateX += e.Delta.Translation.X;
    _Transform.TranslateY += e.Delta.Translation.Y;
}

Best of luck!

like image 90
Jerry Nixon Avatar answered Mar 20 '23 03:03

Jerry Nixon