Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas manipulation event not firing until after first try

I'm trying to animate a canvas by swiping using ´ManipulationDelta`. The problem is simple. when I run the app, the only time the manipulation event is not fired is at the first try. If I tap (or swipe) anywhere in the canvas once, then the second time it works until the application is restarted.

This is my XAML code:

<Canvas Name="rootCanvas" Style="{StaticResource rootCanvas}" Hold="rootCanvas_Hold" ManipulationDelta="rootCanvas_ManipulationDelta" ManipulationCompleted="rootCanvas_ManipulationCompleted">
    <Canvas.Resources>
        <Storyboard x:Name="menuAnimation">
            <DoubleAnimation Duration="0:0:0.2" To="0" Storyboard.TargetProperty="(Canvas.Left)" Storyboard.TargetName="rootAnimation" d:IsOptimized="True" />
        </Storyboard>
    </Canvas.Resources>
    <Canvas Name="rootAnimation">
        <Grid x:Name="rootContainer" DataContext="{Binding}" Style="{StaticResource rootContainer}">
            ...
        </grid>
    </Canvas>
</Canvas>

And this is my code in C#

private void rootCanvas_ManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
{
    if (e.DeltaManipulation.Translation.X > 25 && !menu.IsMenuOpen())
    {
        MoveViewWindow(150);
        hintActive = false;
        menu.SetMenuOpen();
        return;
    }
    else if (e.DeltaManipulation.Translation.X < (-25) && menu.IsMenuOpen())
    {
        MoveViewWindow(0);
        hintActive = false;
        menu.SetMenuClosed();
        return;
    }
}

private void rootCanvas_Hold(object sender, System.Windows.Input.GestureEventArgs e)
{
    if (!menu.IsMenuOpen())
    {
        hintActive = true;
        MoveViewWindow(25);
    }
    return;
}

I have a rootCanvas_ManipulationCompleted where the breakpoint gets hit even if the ManipulationDelta and Hold breakpoints aren't.

Any ideas?

like image 961
DerpyNerd Avatar asked Nov 11 '22 07:11

DerpyNerd


1 Answers

Try the command:

rootCanvas.Focus();

This will set focus to the canvas you are hitting, just pop this event in the load method of the page.

like image 153
James Kirk Avatar answered Nov 14 '22 23:11

James Kirk