WP7:处理复合元素的输入
本文关键字:输入 元素 复合 处理 WP7 | 更新日期: 2023-09-27 18:11:12
我正在尝试使用这个ControlTemplate
:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:svetlin="clr-namespace:SvetlinAnkov.examples">
<Style TargetType="svetlin:MyCompositeControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="svetlin:MyCompositeControl">
<Grid x:Name="ContentGrid">
<Button>Click me</Button>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
我想"拦截"触摸事件,使它们不是由按钮处理,而是由MyCompositeControl
处理:
namespace SvetlinAnkov.examples
{
public class MyCompositeControl : Control
{
private Grid contentGrid;
public MyCompositeControl()
{
DefaultStyleKey = typeof(MyCompositeControl);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
contentGrid = GetTemplateChild("ContentGrid") as Grid;
contentGrid.ManipulationStarted +=
new EventHandler<ManipulationStartedEventArgs>(
contentGrid_ManipulationStarted);
// The same for Delta & Completed
}
protected override void OnManipulationStarted(
ManipulationStartedEventArgs e)
{
Debug.WriteLine("OnManipulationStarted");
}
private void contentGrid_ManipulationStarted(
object sender, ManipulationStartedEventArgs e)
{
e.Handled = true;
Debug.WriteLine("ManipulationStarted");
}
}
}
然而,即使我将Handler
设置为True
,按钮仍然得到它。我想,它是先得到它的。
然后我认为设置按钮上的IsHitTestVisible
到False
会做到这一点。这一次,contentGrid_ManipulationStarted
和MyCompositeControl.OnManipulationStarted
都没有被调用。
谢谢!
显然,这非常简单。只需要考虑这样一个事实,即Grid
将元素一个堆叠在另一个之上,并且Background
设置为Transparent
的元素仍然接收输入事件。
这是ResourceDictionary
:只是添加了Border
控件:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:svetlin="clr-namespace:SvetlinAnkov.examples">
<Style TargetType="svetlin:MyCompositeControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="svetlin:MyCompositeControl">
<Grid>
<Button IsHitTestVisible="False">Click me</Button>
<Border Background="Transparent" />
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
c#文件实际上是简化的:
namespace SvetlinAnkov.examples
{
public class MyCompositeControl : Control
{
public MyCompositeControl()
{
DefaultStyleKey = typeof(MyCompositeControl);
}
protected override void OnManipulationStarted(
ManipulationStartedEventArgs e)
{
Debug.WriteLine("OnManipulationStarted");
}
}
}