FlipView SelectionChanged事件仅在触摸操作完成时发生

Der*_*tie 9 xaml flipview windows-8.1 windows-phone-8.1

来自文档:

注意当用户使用触摸交互翻转FlipView内容时,只有在触摸操作完成时才会发生SelectionChanged事件.这意味着当用户快速翻阅内容时,并不总是为每个项目生成单个SelectionChanged事件,因为操作仍在进行中.

有没有办法配置FlipView控件SelectionChanged为每次翻转启动?这种行为使得实现分页很有趣,因为如果用户足够快地翻转,可以在添加更多项目之前翻转到列表的末尾.

yas*_*sen 10

该问题的一个解决方案是扩展FlipView和监控它ScrollViewer.这是我建议的快速示例.似乎在水平翻转视图上工作(没有处理任何其他情况,并且没有测试太多).

public class FixedFlipView : FlipView {
    public ScrollViewer ScrollViewer {
        get;
        private set;
    }

    protected override void OnApplyTemplate() {
        base.OnApplyTemplate();

        this.ScrollViewer = (ScrollViewer)this.GetTemplateChild("ScrollingHost");
        this.ScrollViewer.ViewChanged += ScrollViewer_ViewChanged;
    }

    void ScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e) {
        var index = (int)this.ScrollViewer.HorizontalOffset - 2;
        if (this.SelectedIndex != index) {
            this.SelectedIndex = index;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有些事情需要注意:

  1. 您可能希望以不依赖于其名称的不同方式获取ScrollViewer.就像在我的答案使用该方法在这里.虽然,我猜这也很好.

  2. 为此使用单独的事件可能是更好的主意.在上面的代码中,我设置了SelectedIndex属性,它引发了SelectionChanged事件,但它也很可能也在做其他事情,因此在某些情况下可能会出现问题.