【问题标题】:Error 1 No overload for <method> matches delegate 'System.Windows.Input.KeyEventHandler'错误 1 ​​<method> 没有重载匹配委托“System.Windows.Input.KeyEventHandler”
【发布时间】:2015-05-05 12:27:18
【问题描述】:

我尝试在整个网站上搜索,但无法真正找到这个问题的答案,我正在尝试使用箭头键移动我的 Ellipse,但无论我尝试什么,我都会遇到同样的错误。

此外,当我将其放入 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);

        }   
    }
}

【问题讨论】:

    标签: c# methods visual-studio-2013 overloading


    【解决方案1】:

    您正在使用 Windows 窗体中的 KeyEventArgs。而是使用:

    ..., System.Windows.Input.KeyEventArgs key)...
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多