日期:2014-05-17  浏览次数:20804 次

keyDown事件问题
正在学习silverlight的游戏开发
第二讲书中代码实现了鼠标移动控件
联系题然我们自己实践用键盘移动
我编写的代码没有报错但是按下up键没有反应请大家帮我看下问题在哪里
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


namespace SilverlightApplication2
{
    public partial class MainPage : UserControl
    {
        Rectangle rectangle = new Rectangle()
        {
            Fill=new SolidColorBrush(Colors.Green),
            Width=50,
            Height=50


        };

        public MainPage()
        {
            InitializeComponent();
            LayoutRoot.Children.Add(rectangle);
            LayoutRoot.MouseLeftButtonDown += new MouseButtonEventHandler(LayoutRoot_MouseLeftButtonDown);
            LayoutRoot.KeyDown += new KeyEventHandler(LayoutRoot_KeyDown);
        }

        void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == ((int)Key.Up))
            {
                Point p = new Point();
                p.X = Canvas.GetLeft(rectangle);
                p.Y = Canvas.GetTop(rectangle);
                Storyboard storyboard = new Storyboard();
                DoubleAnimation upAnimation = new DoubleAnimation()
                {

                    From = Canvas.GetTop(rectangle),
                    To = (p.Y - 100),
                    Duration = new Duration(TimeSpan.FromMilliseconds(50))

                };
      &n