我尝试在整个网站上搜索,但无法真正找到这个问题的答案,我正在尝试使用箭头键使我的椭圆移动,但无论我尝试什么,我都会遇到同样的错误。
另外,当我把它放在XAML中的Canvas代码中时,我得到了这个错误
KeyDown="movement"
错误:
No overload for 'movement' matches delegate 'System.Windows.Input.KeyEventHandler'
代码:
public MainWindow()
{
InitializeComponent();
x = 10;
y = 10;
diameter = 10;
DrawBall(x, y, diameter);
}
private void movement(object sender, System.Windows.Forms.KeyEventArgs key)
{
if (key.KeyCode == Keys.Up)
{
x -= 20;
}
else if (key.KeyCode == Keys.Down)
{
x += 20;
}
else if (key.KeyCode == Keys.Left)
{
y -= 20;
}
else if (key.KeyCode == Keys.Right)
{
y += 20;
}
DrawBall(x, y, diameter);
}
private void DrawBall(double x, double y, double diameter)
{
ballCanvas.Children.Clear();
Ellipse ellipse = new Ellipse();
ellipse.Stroke = new SolidColorBrush(Colors.Black);
ellipse.Fill = new SolidColorBrush(Colors.Red);
ellipse.Width = diameter;
ellipse.Height = diameter;
ellipse.Margin = new Thickness(x, y, 0, 0);
ballCanvas.Children.Add(ellipse);
}
}
}
答案 0 :(得分:1)
您正在使用Windows窗体中的KeyEventArgs。而是使用:
..., System.Windows.Input.KeyEventArgs key)...