Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 当按键“0”时调用函数;“空间”;被压在窗户里_Wpf - Fatal编程技术网

Wpf 当按键“0”时调用函数;“空间”;被压在窗户里

Wpf 当按键“0”时调用函数;“空间”;被压在窗户里,wpf,Wpf,我正在开发一个棋盘游戏,我想在按下窗口中的空格时调用一个函数,但在搜索过程中找不到问题的答案。 你有什么想法吗?有很多方法可以做到这一点。最简单的方法是连接一个事件处理程序(尽管不是最优雅的)。不过,也可以使用涉及命令的更优雅的解决方案,具体取决于您使用的控件 XAML XAML: <Window KeyDown="MainWindow_OnKeyDown" <!-- other properties --> > <!-- rest of y

我正在开发一个棋盘游戏,我想在按下窗口中的空格时调用一个函数,但在搜索过程中找不到问题的答案。
你有什么想法吗?

有很多方法可以做到这一点。最简单的方法是连接一个事件处理程序(尽管不是最优雅的)。不过,也可以使用涉及命令的更优雅的解决方案,具体取决于您使用的控件

XAML

XAML:

<Window KeyDown="MainWindow_OnKeyDown"
        <!-- other properties -->
>
    <!-- rest of your UI -->
</Window>
private void MainWindow_OnKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Space)
    {
        // do something  
    }
}
<Window ...
        PreviewKeyUp="Window_PreviewKeyUp" >


</Window>
// Use the PreviewKeyUp event to capture the keypress before 
// any child controls that have focus handle the event
private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
{
    if(e.Key == Key.Space)
    {
        AFunction(); 
        // to prevent the key press to bubble up to child controls that have focus
        e.Handled = true; 
    }
}