我正在尝试在我的UWP应用中水平滑动/轻弹。我已按照MSDN链接添加GestureRecognizer
。如果我的XAML页面中只有Grid
和TextBlock
控件,那么效果很好。但是,我的XAML页面中有Grid
和ListBox
。它不适用于ListBox
以下是一个示例;
类GestureInputProcessor { Windows.UI.Input.GestureRecognizer gestureRecognizer; UIElement元素;
public GestureInputProcessor(Windows.UI.Input.GestureRecognizer gr, Windows.UI.Xaml.UIElement target)
{
gestureRecognizer = gr;
element = target;
gestureRecognizer.GestureSettings =
Windows.UI.Input.GestureSettings.Tap |
Windows.UI.Input.GestureSettings.Hold | Windows.UI.Input.GestureSettings.RightTap |
Windows.UI.Input.GestureSettings.CrossSlide;
// Set up pointer event handlers. These receive input events that are used by the gesture recognizer.
element.PointerCanceled += OnPointerCanceled;
element.PointerPressed += OnPointerPressed;
element.PointerReleased += OnPointerReleased;
element.PointerMoved += OnPointerMoved;
// Set up event handlers to respond to gesture recognizer output
Windows.UI.Input.CrossSlideThresholds threshold = new Windows.UI.Input.CrossSlideThresholds();
threshold.SelectionStart = 2;
threshold.SpeedBumpStart = 3;
threshold.SpeedBumpEnd = 4;
threshold.RearrangeStart = 5;
gestureRecognizer.CrossSlideHorizontally = true;
gestureRecognizer.CrossSlideThresholds = threshold;
gestureRecognizer.CrossSliding += GestureRecognizer_CrossSliding;
}
private void GestureRecognizer_CrossSliding(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.CrossSlidingEventArgs args)
{
MessageService.showMessage("You are here", MessageType.Information);
}
void OnPointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs args)
{
// Route teh events to the gesture recognizer
gestureRecognizer.ProcessDownEvent(args.GetCurrentPoint(element));
// Set the pointer capture to the element being interacted with
element.CapturePointer(args.Pointer);
// Mark the event handled to prevent execution of default handlers
args.Handled = true;
}
void OnPointerCanceled(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs args)
{
gestureRecognizer.CompleteGesture();
args.Handled = true;
}
void OnPointerReleased(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs args)
{
gestureRecognizer.ProcessUpEvent(args.GetCurrentPoint(element));
args.Handled = true;
}
void OnPointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs args)
{
gestureRecognizer.ProcessMoveEvents(args.GetIntermediatePoints(element));
}
}
XAML Page;
<Grid x:Name="LayoutRoot" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<ListBox x:Name="lsbReadingChapter" ItemsSource="{Binding ChapterContent}" SelectedItem="{Binding SelectedAya}" DoubleTapped="lsbReadingChapter_DoubleTapped"
FlowDirection="RightToLeft" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Visibility="{Binding Visiblity}" HorizontalAlignment="Stretch" FlowDirection="{Binding TranslationLanguage.FlowDirection}" FontSize="{Binding TranslationFont.FontSize}" TextWrapping="Wrap" FontFamily="{Binding TranslationFont.FontPath}" Text="{Binding AyaTranslation}">
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
XAML Page Contstructor
//Handle horizental / vertical swipe event
GestureRecognizer gr1 = GestureRecognizer();
public MainPage()
{
//swipe event this works well, If I have TextBlock instead of ListBox
GestureInputProcessor ShapeInput1 = new GestureInputProcessor(gr1, LayoutRoot);
}
指针事件也在ListBox
中触发,但手势不起作用。我希望在我的Listbox
控件中启用向左/向右滑动功能。有可能吗?
谢谢!