1

我为应用程序 WP8 制作了一个简单的自定义翻转视图,效果很好,但我不知道如何在没有箭头的情况下给出滑动手势来更改图像,这是代码(在本主题WinRT FlipView 中的答案之后,如控制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# 开发的菜鸟)

4

1 回答 1

1

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

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

<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

计算你的手势。

于 2014-10-31T01:34:45.283 回答