[英]WP8 custom flipView C# with swipe gesture
我为应用程序WP8制作了一个简单的自定义flipView,效果很好,但是我不知道如何通过滑动手势来更改没有箭头的图像,这是代码(以下为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#开发中是菜鸟)
通过使用NuGet或访问其网站The Windows Phone Toolkit来获取Windows Pone工具包 。
然后,您可以使用想要检测滑动的控件上的<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
计算您的手势。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.