How to programmatically navigate in a FlipView with swipe animations

会有一股神秘感。 提交于 2019-12-05 22:47:11

问题


The Windows Dev Center states regarding the UseTouchAnimationsForAllNavigation property:

The XAML FlipView control supports three modes of navigation; touch-based, button-based and programmatic. When a user navigates by touch, the FlipView items scroll smoothly into view. When you set this property to true, the same navigation animation occurs whether the navigation is touch-based, button-based and programmatic.

I'm currently navigating from my page's code behind by assigning the SelectedItem property of the FlipView:

FlipView.SelectedItem = FlipView.Items.Last();

However, the swipe animation does not show. How can I enable it?


回答1:


Meanwhile, I was able to solve this problem. I have a button that triggers the navigation to the next FlipViewItem. This button however was placed in a FlipViewItem.

With my setup (touch device), nothing happend. Then I tried clicking the button with the mouse and it worked. After I disabled UseTouchAnimationsForAllNavigation, it also worked using touch input. In my tests, I placed the button outside of the FlipView and it did work using animations.

Here's the problem: When tapping the button, the navigation animation tries to start (SelectedIndex is set correctly), but stopped because the user blocks the animation by still touching the button. So, the navigation is cancelled and SelectionChanged reports the current page.

The solution is to set ManipulationMode of the Button to All. After that, you can't flip the FlipViewItem when touching the button, but the animation executes and it works like charm.




回答2:


I solved the same problem in a different way. As Chliebel said, it is because your finger is still touching the control, so flipview cannot animate. So I gave a small break after before navigating. By the time user would have released the finger and it works !!!

        await Task.Delay(100);
        flipView.SelectedIndex += 1;



回答3:


For me, ChristiaanV's answer has helped:

Animation only occurs when you navigate to the previous or next item in the FlipView.

So I've set up a while loop to set the selected index to the previous / next one until I reach the desired page.

Example Code:

If you'd like to reach the first page:

while(flipView.SelectedIndex > 0)
{
    flipView.SetValue(FlipView.SelectedIndexProperty, flipView.SelectedIndex - 1);
}



回答4:


Here's a compact solution I found, similar to Bhawk1990's:

//nb is the index you wish to get to.

if (nb > flipview.SelectedIndex)
    while (flipview.SelectedIndex != nb)
        flipview.SelectedIndex++;
else if (nb < flipview.SelectedIndex)
    while (flipview.SelectedIndex != nb)
        flipview.SelectedIndex--;



回答5:


I spent a couple of days figuring out why touch animation does not work when I programmatically change FlipView selected item when UseTouchAnimationsForAllNavigation="True".

I found a setting which controls animation machine wide. Somehow this setting was disabled:

Settings->Visual options->Play animations in Windows


来源:https://stackoverflow.com/questions/21305268/how-to-programmatically-navigate-in-a-flipview-with-swipe-animations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!