繁体   English   中英

WPF InputSimulator 在用户控件上不起作用

[英]WPF InputSimulator not working on a User Control

目标是制作一个用户控制键盘,所以我从 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文件中,这样就没有问题了。 问题是,我发现如果我单击按钮的文本(属性内容)它可以工作,但是如果我单击按钮的 rest 空间它不起作用,请参见此图: 在此处输入图像描述

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

<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>

所以我想解决办法是想办法去除hover效应,还有什么办法可以去除??

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

 <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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM