WP8自定义flipView C#用滑动手势

时间:2014-10-30 19:30:39

标签: c# windows-phone-8 flipview

我为应用程序WP8制作了一个简单的自定义flipView,这个工作正常,但我不知道如何在不使用箭头的情况下进行滑动手势来更改图像,这里是代码(在本主题中回答如下) WinRT FlipView like control in WP8):

namespace PhoneApp1
{
public partial class FlipView : UserControl
{
    public FlipView()
    {
        InitializeComponent();
        Datasource = new List<object>();
        SelectedIndex = 0;
    }

    private IList Datasource;
    public static readonly DependencyProperty ItemTemplateProperty =
        DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(FlipView), new   PropertyMetadata(default(DataTemplate)));

    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set
        {
            SetValue(ItemTemplateProperty, value);
            contentPresenter.ContentTemplate = value;
            contentPresenter.Content = SelectedItem;
        }
    }

    public static readonly DependencyProperty ItemsSourceProperty =
       DependencyProperty.Register("ItemsSource", typeof(IList), typeof(FlipView), new PropertyMetadata(default(IList)));

    public IList ItemsSource
    {
        get { return (IList)GetValue(ItemsSourceProperty); }
        set
        {
            SetValue(ItemsSourceProperty, value);
            Datasource = value;
            SelectedIndex = SelectedIndex;
        }
    }

    public static readonly DependencyProperty SelectedIndexProperty =
        DependencyProperty.Register("SelectedIndex", typeof(int), typeof(FlipView), new PropertyMetadata(default(int)));

    public int SelectedIndex
    {
        get { return (int)GetValue(SelectedIndexProperty); }
        set
        {
            SetValue(SelectedIndexProperty, value);

            rightButton.Visibility = leftButton.Visibility = Visibility.Visible;
            if (SelectedIndex == 0)
            {
                leftButton.Visibility = Visibility.Collapsed;
            }

            if (SelectedIndex + 1 == Datasource.Count)
            {
                rightButton.Visibility = Visibility.Collapsed;
                SelectedItem = Datasource[SelectedIndex];
            }

            if (Datasource.Count > SelectedIndex + 1)
            {
                SelectedItem = Datasource[SelectedIndex];
            }
        }
    }

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem", typeof(object), typeof(FlipView), new PropertyMetadata(default(object)));

    public object SelectedItem
    {
        get { return (object)GetValue(SelectedItemProperty); }
        set
        {
            SetValue(SelectedItemProperty, value);
            contentPresenter.Content = SelectedItem;
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SelectedIndex--;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        SelectedIndex++;
    }
}
}

为这个自定义翻转视图提供滑动手势的最佳做法是什么? (顺便说一句,我是C#开发中的noob)

1 个答案:

答案 0 :(得分:1)

使用NuGet获取Windows Pone Toolkit或访问他们的网站The Windows Phone Toolkit

然后您可以使用控件上的<toolkit:GestureService.GestureListener>来检测滑动,如下所示:

<Image Source="/Assets/AlignmentGrid.png">
    <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener Flick="OnFlick"></toolkit:GestureListener>
    </toolkit:GestureService.GestureListener>
</Image>
private void OnFlick(object sender, FlickGestureEventArgs e)
{
    double swipe_velocity = 1000;

    // User flicked towards left
    if (e.HorizontalVelocity < -swipe_velocity)
    {
        // Load the next image 
    }

    // User flicked towards right
    if (e.HorizontalVelocity > swipe_velocity)
    {
        // Load the previous image
    }
}

您可以将swipe_velocity更改为您认为合适的值。

如果没有工具包,您必须使用XNA或使用3个​​事件

ManipulationCompleted
ManipulationDelta
ManipulationStarted

计算你的手势。