我正在尝试制作一个简单的键盘,用于在触摸显示器上输入密码。第一次触摸按钮时会突出显示该按钮,但不会触发Command. 第一次按下后,所有按钮均正常工作。当我用鼠标单击按钮时,即使是第一次单击,它们也能正常工作。
我认为这与焦点有关,我已经Focusable="False"在 中进行了设置Style,但第一次触摸仍然会聚焦按钮。我想完全禁用焦点并且不突出显示按钮。
我的 XAML 代码:
<UserControl x:Class="TestShell.Dialogs.ExitApplicationDialog"
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:TestShell.Dialogs"
xmlns:prism="http://prismlibrary.com/"
mc:Ignorable="d"
Background="White"
BorderThickness="2"
d:DesignHeight="400" d:DesignWidth="300">
<UserControl.Resources>
<Style x:Key="TouchButton" TargetType="{x:Type Button}">
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="ClickMode" Value="Press"/>
<Setter Property="Margin" Value="3"/>
<Setter Property="FontSize" Value="48"/>
</Style>
</UserControl.Resources>
<prism:Dialog.WindowStyle>
<Style TargetType="Window">
<Setter Property="Height" Value="400" />
<Setter Property="Width" Value="300"/>
<Setter Property="ResizeMode" Value="NoResize" />
<Setter Property="prism:Dialog.WindowStartupLocation" Value="CenterScreen" />
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="2"/>
</Style>
</prism:Dialog.WindowStyle>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Content="{Binding DisplayedPasscode}" Grid.ColumnSpan="3" Margin="10" FontSize="58" BorderBrush="Black" BorderThickness="1" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<Button Grid.Row="1" Grid.Column="0" Content="1" Style="{StaticResource TouchButton}" Command="{Binding KeyPressCommand}" CommandParameter="1"/>
<Button Grid.Row="1" Grid.Column="1" Content="2" Style="{StaticResource TouchButton}" Command="{Binding KeyPressCommand}" CommandParameter="2"/>
<Button Grid.Row="1" Grid.Column="2" Content="3" Style="{StaticResource TouchButton}" Command="{Binding KeyPressCommand}" CommandParameter="3"/>
<Button Grid.Row="2" Grid.Column="0" Content="4" Style="{StaticResource TouchButton}" Command="{Binding KeyPressCommand}" CommandParameter="4"/>
<Button Grid.Row="2" Grid.Column="1" Content="5" Style="{StaticResource TouchButton}" Command="{Binding KeyPressCommand}" CommandParameter="5"/>
<Button Grid.Row="2" Grid.Column="2" Content="6" Style="{StaticResource TouchButton}" Command="{Binding KeyPressCommand}" CommandParameter="6"/>
<Button Grid.Row="3" Grid.Column="0" Content="7" Style="{StaticResource TouchButton}" Command="{Binding KeyPressCommand}" CommandParameter="7"/>
<Button Grid.Row="3" Grid.Column="1" Content="8" Style="{StaticResource TouchButton}" Command="{Binding KeyPressCommand}" CommandParameter="8"/>
<Button Grid.Row="3" Grid.Column="2" Content="9" Style="{StaticResource TouchButton}" Command="{Binding KeyPressCommand}" CommandParameter="9"/>
<Button Grid.Row="4" Grid.Column="1" Content="0" Style="{StaticResource TouchButton}" Command="{Binding KeyPressCommand}" CommandParameter="0"/>
<!--<Button Grid.Row="4" Grid.Column="2" Content="Cancel" Margin="3" FontSize="24" Command="{Binding CancelDialogCommand}"/>
<Button Grid.Row="4" Grid.Column="0" Content="" Margin="3" FontSize="24"/>-->
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
我正在使用IDialogService.ShowDialogPrism 库中的内容来显示键盘。
更新1
我尝试使用以下代码将焦点设置在托管键盘的 Windows 上UserControl,但没有任何区别。
public partial class ExitApplicationDialog : UserControl
{
public ExitApplicationDialog()
{
InitializeComponent();
Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
{
Window yourParentWindow = Window.GetWindow(this);
yourParentWindow.Activate();
yourParentWindow.Focus();
}));
}
}
Run Code Online (Sandbox Code Playgroud)
更新2
我曾经Snoop检查第一次触摸之前和之后焦点在哪里。Snoop 表示第一次按下前后焦点均处于打开状态DialogWindow。
更新3
我现在正在考虑创建一个自定义按钮,该按钮也会在第一次按下时触发命令。我通过附加到该事件进行了测试PreviewTouchDown。
private int index = 0;
private void Button_PreviewTouchDown(object sender, TouchEventArgs e)
{
Debug.WriteLine($"PreviewTouchDown {index}");
index++;
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,我第一次按下按钮时(当它没有触发时Command),它只触发事件一次,而每次按下其他按钮都会触发事件两次。
更新4
触摸和鼠标单击的区别在于,第一次触摸单击会触发 和PreviewTouchDown事件TouchDown,而每次其他触摸单击也会触发PreviewMouseDown和Click事件。通过单击鼠标,总是会触发PreviewMouseDown和事件。Click
| 首次触摸按下 | 跟随触摸按键 |
|---|---|
|
|
| 第一次鼠标点击 | 跟随鼠标点击 |
|---|---|
|
|
| 归档时间: |
|
| 查看次数: |
468 次 |
| 最近记录: |