c# - WPF/C# - 对话框窗口不响应触摸

标签 c# wpf windows dialog touch

我正在开发一个 C# WPF 应用程序,该应用程序旨在使用专门的触摸功能在完整的 Windows 10 平板电脑上运行。到目前为止,该应用程序运行良好,除了我的一个对话框窗口有一些按钮让人不喜欢被触摸。

对话框窗口 XAML:

<Window x:Class="Commencement_Organizer.ConfirmStudentWindow"
    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:Commencement_Organizer"
    mc:Ignorable="d"
    Title="Confirmation" Topmost="True" Height="372.677" Width="578.225" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" 
    WindowStyle="None" Background="White" AllowsTransparency="True" Stylus.IsTapFeedbackEnabled="False" Stylus.IsTouchFeedbackEnabled="False">

<Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation Duration="00:00:0.2" Storyboard.TargetProperty="Opacity" From="0" To="1" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Window.Triggers>

<Grid Background="#FF171717">
    <Grid Margin="1" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Button x:Name="YesButton" Content="Yes" Margin="25" Grid.Row="2" Click="YesButton_Click"/>
        <Button x:Name="NoButton" Content="No" Margin="25" Grid.Row="2" Grid.Column="1" Click="NoButton_Click"/>
        <Label x:Name="label" Content="Confirm your name" Margin="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24"/>
        <Label x:Name="nameLabel" Content="Label" HorizontalAlignment="Center" Margin="0" Grid.Row="1" VerticalAlignment="Center" Grid.ColumnSpan="2" FontSize="24" FontWeight="Bold"/>

    </Grid>
</Grid>

实现(C#):

ConfirmStudentWindow confirmWindow = new ConfirmStudentWindow(student);
        confirmWindow.Confirm += OnConfirm;

        // This window makes everything behind the dialog window a grey tint, which makes the dialog more prominent
        var darkwindow = new Window() 
        {
            Background = Brushes.Black,
            Opacity = 0.4,
            AllowsTransparency = true,
            WindowStyle = WindowStyle.None,
            WindowState = WindowState.Maximized,
            Topmost = true,
            Effect = new BlurEffect()
        };
        darkwindow.Show(); // Show grey background tint
        confirmWindow.ShowDialog(); // Stops main UI thread
        darkwindow.Close();

一切都按预期工作,除了当我尝试使用触摸屏来使用这些按钮时,它们只是保持突出显示,但实际上并没有点击。与鼠标完美搭配。

在 2 种不同的 Windows 10 设备(Surface Pro 2 和 Surface Book)上进行了测试。

以问题的形式表达:

为什么将此窗口作为对话框启动会使其无法触摸,但如果将其作为常规窗口启动则不会?

编辑 - 新问题:

除了启动一个始终位于顶部的常规窗口然后在其后面放置一个有色覆盖层同时为该窗口提供一个提供用户输入结果的事件处理程序之外,还有什么方法可以模拟对话窗口?

最佳答案

我试过你的例子,它在我这边运行没有任何问题。我正在使用带有 W10 和 .NET 4.5.2 触摸屏的 DELL 笔记本电脑。我可以建议您的一件事是尝试在对话框中的按钮上连接 StylusUp(或取决于您的逻辑的 StylusDown)事件。我以前在只管理点击事件的时候确实遇到过类似的问题。

这对我有用,调用了确认方法并关闭了对话框。

ConfirmStudentWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Input;

namespace DialogTouchTest
{
    /// <summary>
    /// Interaction logic for ConfirmStudentWindow.xaml
    /// </summary>
    public partial class ConfirmStudentWindow : Window
    {
        public Action Confirm;

        public ConfirmStudentWindow()
        {
            InitializeComponent();
        }

        private void YesButton_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            Yes();
        }

        private void NoButton_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            No();
        }

        private void YesButton_StylusUp(object sender, StylusEventArgs e)
        {
            e.Handled = true;
            Yes();
        }

        private void NoButton_StylusUp(object sender, StylusEventArgs e)
        {
            e.Handled = true;
            No();
        }

        private void Yes()
        {
            DialogResult = true;
            Confirm();
        }

        private void No()
        {
            DialogResult = false;
        }
    }
}

ConfirmStudentWindow.xaml

<Window x:Class="DialogTouchTest.ConfirmStudentWindow"
        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"
    mc:Ignorable="d"
    Title="Confirmation" Topmost="True" Height="372.677" Width="578.225" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" 
    WindowStyle="None" Background="White" AllowsTransparency="True" Stylus.IsTapFeedbackEnabled="False" Stylus.IsTouchFeedbackEnabled="False">

    <Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Duration="00:00:0.2" Storyboard.TargetProperty="Opacity" From="0" To="1" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>

    <Grid Background="#FF171717">
        <Grid Margin="1" Background="White">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Button x:Name="YesButton" Content="Yes" Margin="25" Grid.Row="2" StylusUp="YesButton_StylusUp" Click="YesButton_Click"/>
            <Button x:Name="NoButton" Content="No" Margin="25" Grid.Row="2" Grid.Column="1" StylusUp="NoButton_StylusUp" Click="NoButton_Click"/>
            <Label x:Name="label" Content="Confirm your name" Margin="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24"/>
            <Label x:Name="nameLabel" Content="Label" HorizontalAlignment="Center" Margin="0" Grid.Row="1" VerticalAlignment="Center" Grid.ColumnSpan="2" FontSize="24" FontWeight="Bold"/>

        </Grid>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;

namespace DialogTouchTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            ConfirmStudentWindow confirmWindow = new ConfirmStudentWindow();
            confirmWindow.Confirm += OnConfirm;

            // This window makes everything behind the dialog window a grey tint, which makes the dialog more prominent
            var darkwindow = new Window()
            {
                Background = Brushes.Black,
                Opacity = 0.4,
                AllowsTransparency = true,
                WindowStyle = WindowStyle.None,
                WindowState = WindowState.Maximized,
                Topmost = true,
                Effect = new BlurEffect()
            };
            darkwindow.Show(); // Show grey background tint
            confirmWindow.ShowDialog(); // Stops main UI thread
            darkwindow.Close();
        }

        private void OnConfirm()
        {

        }
    }
}

关于c# - WPF/C# - 对话框窗口不响应触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39691998/

相关文章:

c# - 你将如何声明 DLL 导入签名?

c# - _Layout.cshtml不能直接请求,因为调用了 "RenderBody"方法

wpf - 扩展的 WPF 工具包 : Show items in PropertyGrid

windows - Powershell ConvertTo-SecureString ObjectNotFound

windows - 在 Windows 批处理文件中检查非零(错误)返回码的万无一失的方法

c# - 如何从文本框/标签中删除文本而不闪烁

c# - 从数组值声明 "variable variables"或 "dynamic variables"?

c# - 如何在 ResourceDictionary 中使用转换器

c# - 更改按钮控件模板,现在鼠标悬停不起作用

javascript - 从浏览器启动 dialer.exe?