【问题标题】:WPF MVVM Key BindingWPF MVVM 键绑定
【发布时间】:2021-03-01 14:09:54
【问题描述】:

我是 WPF 新手,我编写的代码似乎不起作用。按下左箭头键时,我试图将矩形桨向左移动,但按下左箭头键时没有响应。我在这里做错了什么?

这里是视图:

<UserControl.InputBindings>
    <KeyBinding Key="Left" Command="{Binding MovePaddleLeftCommand}"/>
</UserControl.InputBindings>

这是 ViewModel 中的代码:

private ICommand _movePaddleLeftCommand;
public ICommand MovePaddleLeftCommand
{
    get
    {
        return new DelegateCommand(ExecuteCommand, CanExecute);
    }
}

private void ExecuteCommand()
{
    MessageBox.Show("Left");
    MovePaddleLeft();
}

private bool CanExecute()
{
    return true;
}

如果相关,下面是更完整的代码图片。

GameView.xaml:

<UserControl x:Class="Arkanoid_MkII.Views.GameView"
             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:Arkanoid_MkII.Views"
             mc:Ignorable="d"
             Width="1500" Height="800">
    <Canvas Name="GameArea" ClipToBounds="True">
        <Rectangle Canvas.Left="{Binding Path=Paddle.PaddleX}" Canvas.Top="750" Width="{Binding Path=Paddle.Width}" Height="{Binding Path=Paddle.Height}" Fill="{Binding Path=Paddle.Colour}"/>
    </Canvas>
    <UserControl.InputBindings>
        <KeyBinding Key="Left" Command="{Binding MovePaddleLeftCommand}"/>
    </UserControl.InputBindings>
</UserControl>

GameView.xaml.cs:

public partial class GameView : UserControl
    {
        public GameView()
        {
            InitializeComponent();
        }
    }

MainWindow.xaml:

<Window x:Class="Arkanoid_MkII.MainWindow"
        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:Arkanoid_MkII"
        xmlns:views="clr-namespace:Arkanoid_MkII.Views"
        mc:Ignorable="d"
        Title="MainWindow" SizeToContent="WidthAndHeight" ResizeMode="NoResize" Background="Black" Padding="100">
    <Grid>
        <Border BorderBrush="Black" BorderThickness="5">
            <views:GameView x:Name="GameViewControl" Loaded="GameViewControl_Loaded" />
        </Border>
    </Grid>
</Window>

MainWindow.xaml.cs:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void GameViewControl_Loaded(object sender, RoutedEventArgs e)
        {
            Arkanoid_MkII.ViewModels.GameViewModel gameViewModelObject = new Arkanoid_MkII.ViewModels.GameViewModel();

            GameViewControl.DataContext = gameViewModelObject;
        }
    }

GameViewModel.cs:

private ICommand _movePaddleLeftCommand;
        public ICommand MovePaddleLeftCommand
        {
            get
            {
                return new DelegateCommand(ExecuteCommand, CanExecute);
            }
        }

        private void ExecuteCommand()
        {
            MessageBox.Show("Left");
            MovePaddleLeft();
        }

        private bool CanExecute()
        {
            return true;
        }

【问题讨论】:

  • paddel相关属性是如何定义的,MovePaddleLeft方法有什么作用?
  • 桨被定义为它自己的桨类,它有一个属性“PaddleX”,它定义了它在X轴上的位置。 MovePaddleLeft 方法减少了 PaddleX 值,但我认为这不是问题所在,因为 MessageBox 甚至没有出现,并且当我按下左键时,MovePaddleLeftCommand 中的断点没有被命中

标签: c# .net wpf xaml mvvm


【解决方案1】:

UserControl 必须是可聚焦的,并且要执行命令:

private void GameViewControl_Loaded(object sender, RoutedEventArgs e)
{
    Arkanoid_MkII.ViewModels.GameViewModel gameViewModelObject = 
        new Arkanoid_MkII.ViewModels.GameViewModel();

    GameViewControl.DataContext = gameViewModelObject;

    GameViewControl.Focusable = true;
    Keyboard.Focus(GameViewControl);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多