MessageBox锁定TouchInput

时间:2017-04-20 10:20:07

标签: c# wpf mvvm touch messagebox

请看这个蹩脚的WPF MVVM应用程序:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>

    <StackPanel>
        <Ellipse Fill="Black" Height="40" Width="40">
            <Ellipse.InputBindings>
                <MouseBinding Gesture="LeftClick" Command="{Binding Pippo}" />
            </Ellipse.InputBindings>
        </Ellipse>
    </StackPanel>
</Window>

ViewModel类:

class ViewModel
{
    public ICommand Pippo { get; set; }

    public ViewModel()
    {
        Pippo = new RelayCommand(x => MessageBox.Show("Here I am"));
    }
}

运行它并用鼠标单击,一切都按预期工作。

但是如果你使用触摸显示器,你触摸椭圆,打开消息框,用ok关闭它,触摸空白区域,消息框再次被触发!切换到鼠标,一切都很好。

为什么触摸输入被锁定?以及如何防止这种情况?

我在两台不同的显示器上验证了这一点,包括Win 7和Win 8。

使用Interactivity而不是MouseBinding没有运气:

<Ellipse Fill="Black" Height="40" Width="40">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="TouchUp" >
            <i:InvokeCommandAction Command="{Binding Pippo}" />
        </i:EventTrigger>
        <i:EventTrigger EventName="MouseDown" >
            <i:InvokeCommandAction Command="{Binding Pippo}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Ellipse>

任何建议都将受到高度赞赏。

修改

我使用了Will建议的Snoop来获取更多信息。在事件列表中,我看到以下内容:当我单击窗口空白区域时,鼠标单击从MainWindow到边框的事件goeas然后返回。 如果我触摸,我有PreviewTouchDown(赢到边框),TouchDown(获胜边框),PreviewTouchMove(赢到边框),TouchMove(获胜边框),PreviewMouseDown(赢取边框到AdornerDecorator到contentpresenter到StackPanel到Ellipse)..等等。 为什么PreviewMouseDown会进入AdornerDecorator并进入Ellipse?

看起来触控输入不会更新触控位置。

编辑2: I found this issue reported here as well,但我不知道如何将该解决方案应用于我的案例。

P.S。我知道在MVVM中,ViewModel不应该提升MessageBoxes。它只是一个简单的例子。

1 个答案:

答案 0 :(得分:1)