wpf - 操作事件未触发

标签 wpf pixelsense

我是在 WPF 中为触摸屏开发的新手,我在解释操作事件时遇到了麻烦。我相信我想做的事情相当简单:当用户捏住 UserControl 上的任何位置时,它将执行一个 Action 。

因此,在我拥有的控件中(这是 Surface 2.0/Windows Touch):

XAML

<Grid Background="White" IsManipulationEnabled="True" 
ManipulationStarting="Grid_ManipulationStarting" 
ManipulationDelta="Grid_ManipulationDelta">
    <!--Some content controls-->
</Grid>

C#
private void Grid_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
    e.ManipulationContainer = this;
}

private void Grid_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
    //do the thing... you know, that thing you do
}

但是,当我在屏幕上摩擦双手时,这些事件都不会触发。我想我一定不明白这种情况下事件的路由。我的显示器(3M MicroTouch PX)在理解触摸事件或 ScatterViewItems 中的内置操作方面没有任何问题。

编辑:我从网格内部删除了控件,它们现在触发了,所以我猜操作被内容拦截了。抱歉,应该更清楚地了解控件的内容,因为它们似乎是问题所在。

具体来说,我认为这与我里面有一个 SurfaceListBox 的事实有关。我想 SurfaceListBox 正在拦截操作。有什么办法可以让它走开吗?我仍在尝试围绕 WPF 执行事件的方式。

Edit2:要粘贴一些更完整的代码。

SEMANTICPANEL.XAML 完整
<UserControl x:Class="SemanticZoom.SemanticPanel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:SemanticZoom"
         xmlns:views="clr-namespace:SemanticZoom.Views"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
</UserControl.Resources>
<Grid Background="White" IsManipulationEnabled="True" ManipulationStarting="Grid_ManipulationStarting" ManipulationDelta="Grid_ManipulationDelta">
    <views:CategoryView x:Name="CategoryView"/>
    <views:ShelfView x:Name="ShelfView" Visibility="Hidden" />
    <views:BookView x:Name="BookView" Visibility="Hidden" />
</Grid>

SEMANTICPANEL.CS 完整
public partial class SemanticPanel : UserControl
{
    public SemanticPanel()
    {
        InitializeComponent();
        CategoryView.CategorySelected += new EventHandler(CategoryView_CategorySelected);
        ShelfView.BookSelected += new EventHandler(ShelfView_BookSelected);
        ShelfView.ZoomOut += new EventHandler(View_ZoomOut);
    }

    void View_ZoomOut(object sender, EventArgs e)
    {
        if (sender == ShelfView)
        {
            ShelfView.Visibility = System.Windows.Visibility.Hidden;
            CategoryView.Visibility = System.Windows.Visibility.Visible;
        }
        else if (sender == BookView)
        {
            BookView.Visibility = System.Windows.Visibility.Hidden;
            ShelfView.Visibility = System.Windows.Visibility.Visible;
        }
    }

    void ShelfView_BookSelected(object sender, EventArgs e)
    {
        BookView.Books = ShelfView.BookList;
        ShelfView.Visibility = System.Windows.Visibility.Hidden;
        BookView.Visibility = System.Windows.Visibility.Visible;
    }

    void CategoryView_CategorySelected(object sender, EventArgs e)
    {
        ShelfView.Category = CategoryView.ActiveCategory;
        ShelfView.Visibility = System.Windows.Visibility.Visible;
        CategoryView.Visibility = System.Windows.Visibility.Hidden;
        ShelfView.RefreshBooks();
    }

    private void Grid_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
    {
        //e.ManipulationContainer = this;
    }

    private void Grid_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    {
        if (e.DeltaManipulation.Scale.X < 0)
        {
            if (ShelfView.Visibility == System.Windows.Visibility.Visible)
            {
                ShelfView.Visibility = System.Windows.Visibility.Hidden;
                CategoryView.Visibility = System.Windows.Visibility.Visible;
            }
            else if (BookView.Visibility == System.Windows.Visibility.Visible)
            {
                BookView.Visibility = System.Windows.Visibility.Hidden;
                ShelfView.Visibility = System.Windows.Visibility.Visible;
            }
        }
    }

CATEGORYVIEW.XAML
<UserControl x:Class="SemanticZoom.Views.CategoryView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:s="http://schemas.microsoft.com/surface/2008"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <!--CATEGORY TEMPLATE-->
    <DataTemplate x:Name="CategoryTemplate" x:Key="CategoryTemplate">
        <s:SurfaceButton Background="Gray" Click="CategoryClicked" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="200" Width="300">
            <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Foreground="White" FontSize="20" FontWeight="Bold" />
        </s:SurfaceButton>
    </DataTemplate>
    <!--CATEGORY STYLE-->
    <Style TargetType="{x:Type s:SurfaceListBoxItem}">
        <Setter Property="Width" Value="300"/>
        <Setter Property="Height" Value="200"/>
    </Style>
</UserControl.Resources>
<Grid>
    <s:SurfaceListBox x:Name="CategoryList" ItemTemplate="{StaticResource CategoryTemplate}">
        <s:SurfaceListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True"
                           Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type ScrollContentPresenter}, Mode=FindAncestor}}"/>
            </ItemsPanelTemplate>
        </s:SurfaceListBox.ItemsPanel>
    </s:SurfaceListBox>
</Grid>

只是不要评判我,因为 a) 是的,我正在尝试模仿 Semantic Zoom,b) 是的,我正在做一个 hacky 工作。我只需要一个简单的工作概念。每个 View 都与 CategoryView 基本相似。

最佳答案

感谢您的帮助 Andriy,但问题似乎很简单,因为我的显示器太不敏感了。在我为操作事件的所有内容删除事件 Hook 后,只在父网格中留下 Hook ,然后用两根手指在屏幕上用力按下,我让事件触发。不过,可能需要在路由上多做一些事情。

关于wpf - 操作事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10079751/

相关文章:

c# - 如何正确地在 wpf 中重用矢量图

.net - 在不使用 WindowsFormsHost 的情况下在 WPF 中查看 PDF

c# - 当某些条件为 True 时,对 WPF 中的数据网格单元格进行动画处理?

c# - 方法 'GetTouchPoint' 没有重载需要 0 个参数

pixelsense - 桌面的多点触控界面有真正的值(value)吗?

c# - WPF 只能打开一个窗口?

c# - 在同一个程序集中引用不同的项目,不同的命名空间

c# - 在 wpf/surface 中从网络加载图像

wpf - WPF 中的环境遮挡着色器效果?

wpf - 如何开始使用 Microsoft PixelSense