触摸 WPF 中的错误?

Posted

技术标签:

【中文标题】触摸 WPF 中的错误?【英文标题】:Touch Bug in WPF? 【发布时间】:2014-12-25 11:28:01 【问题描述】:

我做了以下申请(作为测试)

XAML:

<Window x:Class="GUITest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Background="Transparent">
        <TextBlock Text="openDialog" Background="Red"  HorizontalAlignment="Center" VerticalAlignment="Top" MouseDown="TextBlock_MouseDown" />
    </Grid>
</Window>

C#:

public partial class MainWindow : Window

    public MainWindow()
    
        InitializeComponent();
    

    private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
    
        OpenFileDialog dlg = new OpenFileDialog();

        Nullable<bool> result = dlg.ShowDialog();

        if (result == true)
        
            Console.Out.WriteLine(dlg.FileName);
        
    

我捕获了 mouseDown 事件,因为它捕获了鼠标按钮按下事件和触摸事件。该代码具有鼠标单击的预期行为。触摸给我带来了一些麻烦。

如果我触摸 TextBlock,它会按要求打开一个对话窗口。关闭它后,窗口上的任何触摸都会打开对话框窗口,即使触摸不在 TextBlock 上。

这是一个错误吗?我可以解决这个问题吗?

编辑:我发布了一个解决方法,实际修复仍然有用

【问题讨论】:

我有两个观察结果 - 一,如果你移动窗口,问题就会停止,二,在这样的现实生活中,你可能想要使用一个按钮(我知道这是只是一个测试,所以这是一个有趣的问题)。也许这可以帮助你想出一些东西。 对不起,我找不到任何有用的东西。 @hosch250 感谢按钮提示。这并不是问题的真正答案,而是一种解决方法。我希望按钮看起来像一个 TextBlock,所以我重新设计了按钮,很快发布我的解决方案 【参考方案1】:

对于遇到同样问题的其他人。这不是解决问题的方法,而是一种解决方法。 我使用了一个按钮并将其重新设置为看起来像 TextBlock

XAML:

<Grid Background="Transparent">
   <Button HorizontalAlignment="Left" VerticalAlignment="Top" Click="Button_Click" Content="openDialog">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                 <ContentPresenter />
            </ControlTemplate>
        </Button.Template>
   </Button>
</Grid>

Button_Click 的代码与 TextBlock_MouseDown 相同

【讨论】:

以上是关于触摸 WPF 中的错误?的主要内容,如果未能解决你的问题,请参考以下文章

WPF中的DataGridView绘制错误

Android中的错误“只有创建视图层次结构的原始线程才能触摸其视图”[重复]

WPF .NET Core 中的 gRPC 错误

如何在 WPF 中扩展控件以便为 ErrorTemplate 中的错误消息腾出空间?

在视觉状态之间切换时 WPF 中的致命运行时错误

WPF 插拔触摸设备触摸失效