0

目标是制作一个用户控制键盘,所以我从 NuGet 安装了 InputSimulator,我用一个按钮尝试了它,当然在将该按钮的属性 Focusable 设置为 False 后它运行良好,但是当我将键盘创建为 UserControl 时,键盘不工作(不输入字符)。

这是用户控件的 xaml.cs 中的代码:

private void clickAlphabet(VirtualKeyCode virtualKey)
    {
        var inputSim = new InputSimulator();
        inputSim.Keyboard.KeyPress(virtualKey);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        clickAlphabet(VirtualKeyCode.VK_A);
    }

用户控件的 Xaml(一键):

<Button Content="a" Click="Button_Click"/>

此代码来自 App.xaml:

    <Style TargetType="Button">
        <Setter Property="Focusable" Value="False"/>
    </Style>

这是我如何调用键盘用户控件:

<controls:ucKeyboard Margin="7 300"></controls:ucKeyboard>

这是用户控件的整个 xaml:

<UserControl x:Class="SmartCaisse.PL.UserControls.ucKeyboard"
         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:SmartCaisse.PL.UserControls"
         mc:Ignorable="d" 
         d:DesignHeight="268" d:DesignWidth="797">
<Border CornerRadius="0 0 15 15" Background="green">
    <Grid Width="797">
        <Grid.RowDefinitions>
            <RowDefinition Height="60"/>
            <RowDefinition Height="7"/>
            <RowDefinition Height="60"/>
            <RowDefinition Height="7"/>
            <RowDefinition Height="60"/>
            <RowDefinition Height="7"/>
            <RowDefinition Height="60"/>
        </Grid.RowDefinitions>

        <Grid.Resources>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Margin" Value="0,0,7,0"/>
                <Setter Property="Width" Value="60"/>
                <Setter Property="Height" Value="60"/>
                <Setter Property="Background" Value="{x:Null}"/>
                <Setter Property="Foreground" Value="#fff"/>
                <Setter Property="FontFamily" Value="Tahoma"/>
                <Setter Property="FontSize" Value="20"/>


                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Background="{TemplateBinding Background}" BorderBrush="#fff" BorderThickness="2" Focusable="False">
                                <ContentPresenter HorizontalAlignment="center" VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style TargetType="StackPanel">
                <Setter Property="Orientation" Value="Horizontal"/>
            </Style>
        </Grid.Resources>

        <StackPanel Grid.Row="0">
            <Button Content="a" Click="Button_Click"/>
            <Button Content="z"/>
            <Button Content="e"/>
            <Button Content="r"/>
            <Button Content="t"/>
            <Button Content="y"/>
            <Button Content="u"/>
            <Button Content="i"/>
            <Button Content="o"/>
            <Button Content="p"/>
            <Button Width="127 ">
                <Image Source="/assets/Icons/VirtualKeyboardIcons/backspace.png" Width="24" Height="24"></Image>
            </Button>
        </StackPanel>
        
        <StackPanel Grid.Row="2" HorizontalAlignment="Right">
            <Button Content="q"/>
            <Button Content="s"/>
            <Button Content="d"/>
            <Button Content="f"/>
            <Button Content="g"/>
            <Button Content="h"/>
            <Button Content="j"/>
            <Button Content="k"/>
            <Button Content="l"/>
            <Button Content="m"/>
            <Button Content="Entrée" Width="103" Margin="0"/>
        </StackPanel>
        
        <StackPanel Grid.Row="4">
            <Button>
                <Image Source="/assets/Icons/VirtualKeyboardIcons/arrow-up.png" Width="24" Height="24"></Image>
            </Button>
            <Button Content="w"/>
            <Button Content="x"/>
            <Button Content="c"/>
            <Button Content="v"/>
            <Button Content="b"/>
            <Button Content="n"/>
            <Button Content=","/>
            <Button Content="."/>
            <Button Content="-"/>
            <Button Content="_"/>
            <Button>
                <Image Source="/assets/Icons/VirtualKeyboardIcons/arrow-up.png" Width="24" Height="24"></Image>
            </Button>
        </StackPanel>
        
        <Border Grid.Row="6" CornerRadius="0 0 15 15">
            <StackPanel>
                <Button Content="&amp;123" FontSize="18"/>
                <Button Content="@"/>
                <Button Width="529"/>
                <Button>
                    <Image Source="/assets/Icons/VirtualKeyboardIcons/arrow-left.png" Width="24" Height="24"></Image>
                </Button>
                <Button>
                    <Image Source="/assets/Icons/VirtualKeyboardIcons/arrow-right.png" Width="24" Height="24"></Image>
                </Button>
            </StackPanel>
        </Border>
    </Grid>
</Border>

更新:我发现此代码(在 App.XAML 文件中)对用户控件中的按钮不起作用:

<Style TargetType="Button">
        <Setter Property="Focusable" Value="False"/>
</Style>

所以我在用户控件的XAML文件中重写,这不是问题。问题是,我发现如果我点击按钮的文本(属性内容)它可以工作,但如果我点击按钮的其余空间它不起作用,请看这张图片: 在此处输入图像描述

我找到了产生这个问题的代码,这个来自用户控件的 XAML 文件的代码块,我添加它来删除按钮的默认悬停效果:

<Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Background="{TemplateBinding Background}" BorderBrush="#fff" BorderThickness="2" Focusable="False">
                                <ContentPresenter HorizontalAlignment="center" VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

所以我想解决方案是找到另一种方法来消除悬停效果,还有其他方法可以消除它吗?

4

1 回答 1

0

我将您的按钮样式更新如下,它可以用于单击整个按钮。

 <Style TargetType="{x:Type Button}">
                <Setter Property="Margin" Value="0,0,7,0"/>
                <Setter Property="Width" Value="60"/>
                <Setter Property="Height" Value="60"/>
                <Setter Property="Background" Value="{x:Null}"/>
                <Setter Property="Foreground" Value="#fff"/>
                <Setter Property="FontFamily" Value="Tahoma"/>
                <Setter Property="FontSize" Value="20"/>

                <Setter Property="BorderBrush" Value="#fff"></Setter>
                <Setter Property="BorderThickness" Value="2"></Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Transparent"/>
                    </Trigger>
                </Style.Triggers>
                
            </Style>
于 2021-01-25T09:23:11.390 回答