简体   繁体   中英

WP8 custom flipView C# with swipe gesture

I made a simple custom flipView for app WP8, this works fine, but i don't know how to give a swipe gestures to change the image without the arrows, here is the code (following a answer in this topic 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++;
    }
}
}

What is the best practice to give a swipe gestures a this custom flipview? (by the way, i am noob in C# development)

Get the Windows Pone Toolkit by using NuGet or going to their website The Windows Phone Toolkit .

Then you can use the <toolkit:GestureService.GestureListener> on the Control that you want to detect the Swipe with like so:

<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
    }
}

You can change the swipe_velocity to a value you're comfortable with.

Without the toolkit you would have to use XNA or use the 3 events

ManipulationCompleted
ManipulationDelta
ManipulationStarted

to calculate your Gesture.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM