我有一个UWP应用程序,其中RelativePanel“父”包含另一个RelativePanel“子”。它们都应该能够以不同的方式处理PointerPressed事件。
当我单击父对象时,父对象的PointerPressed事件处理程序被正确触发,但是当我单击子对象时,再次只触发父对象的PointerPressed事件处理程序,以便永远不会调用子对象的PointerPressed事件处理程序。
如何配置子节点以调用其PointerPressed事件处理程序?
这里是创建这些元素的代码的摘录(一切都在后面的代码中完成,因为这些元素应该在运行时创建):
public class ChildView
{
public RelativePanel View { get; set; }
public ChildView()
{
View.Width = 18;
View.Height = 18;
View.SetValue(Canvas.ZIndexProperty, 30); // we want the child always on top of the parent
View.Background = new SolidColorBrush(Colors.Transparent);
// Add the gesture recognizers:
View.Holding += HandleLongPressGesture;
View.PointerPressed += HandleTapGesture;
View.PointerReleased += HandlePointerReleased;
View.DoubleTapped += HandleDoubleTapGesture;
}
public void HandleLongPressGesture(object sender, HoldingRoutedEventArgs e)
{
// my logic
}
private void HandlePointerReleased(object sender, PointerRoutedEventArgs e)
{
// my logic
}
private void HandleDoubleTapGesture(object sender, DoubleTappedRoutedEventArgs e)
{
// my logic
}
private void HandleTapGesture(object sender, PointerRoutedEventArgs e)
{
// my logic
}
}
public class ParentView
{
private ChildView child;
public RelativePanel View { get; set; }
public ParentView()
{
child = new ChildView();
View.Children.Add(child.View);
View.PointerPressed += HandleTapGesture;
View.PointerReleased += HandlePointerReleased;
}
private void HandlePointerReleased(object sender, PointerRoutedEventArgs e)
{
// my logic
}
private void HandleDoubleTapGesture(object sender, DoubleTappedRoutedEventArgs e)
{
// my logic
}
}
将子面板的背景设置为透明。
当事件触发时,确保你执行.Handled = true;在传入的处理程序args上,以防止传播到父控件。
我知道这不是你问题的答案,但也许它会帮助你以某种方式进行调试。
好的,所以我设置了一个简单的项目(在您将代码添加到问题之前)并且无法复制您的问题。
基本上,Child click事件被正确触发,然后Parent click事件立即被触发,这在我的实例中似乎是正确的。
XAML
<Grid>
<RelativePanel Name="Parent" HorizontalAlignment="Left" Height="460" Margin="437,162,0,0" VerticalAlignment="Top" Width="458" Background="#FFFF5C5C" PointerPressed="Parent_PointerPressed">
<RelativePanel Name="Child" HorizontalAlignment="Left" Height="360" Margin="62,50,0,0" VerticalAlignment="Top" Width="334" Background="#FF3B9C9C" PointerPressed="Child_PointerPressed"/>
</RelativePanel>
</Grid>
C#
private void Parent_PointerPressed(object sender, PointerRoutedEventArgs e)
{
// Breaks here when Parent is pressed
// Also breaks here after breaking on Child_PointerPressed
}
private void Child_PointerPressed(object sender, PointerRoutedEventArgs e)
{
// Breaks here when Child is pressed
}