正在学习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)) };
Storyboard.SetTarget(upAnimation, rectangle);
Storyboard.SetTargetProperty(upAnimation, new PropertyPath("(Canvas.Top)"));
storyboard.Children.Add(upAnimation);
storyboard.Begin();
}
} void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point p = e.GetPosition(LayoutRoot);
Storyboard storyboard = new Storyboard();
DoubleAnimation xAnimation = new DoubleAnimation()
{
From = Canvas.GetLeft(rectangle),
To = p.X,
Duration = new Duration(TimeSpan.FromMilliseconds(500))
};
Storyboard.SetTarget(xAnimation, rectangle);
Storyboard.SetTargetProperty(xAnimation, new PropertyPath("(Canvas.Left)"));
storyboard.Children.Add(xAnimation); DoubleAnimation yAnimation = new DoubleAnimation()
{
From = Canvas.GetTop(rectangle),
To = p.Y,
Duration=new Duration(TimeSpan.FromMilliseconds(500))
};
Storyboard.SetTarget(yAnimation, rectangle);
Storyboard.SetTargetProperty(yAnimation, new PropertyPath("(Canvas.Top)"));
storyboard.Children.Add(yAnimation);
storyboard.Begin();
} }
}
第二讲书中代码实现了鼠标移动控件
联系题然我们自己实践用键盘移动
我编写的代码没有报错但是按下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)) };
Storyboard.SetTarget(upAnimation, rectangle);
Storyboard.SetTargetProperty(upAnimation, new PropertyPath("(Canvas.Top)"));
storyboard.Children.Add(upAnimation);
storyboard.Begin();
}
} void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point p = e.GetPosition(LayoutRoot);
Storyboard storyboard = new Storyboard();
DoubleAnimation xAnimation = new DoubleAnimation()
{
From = Canvas.GetLeft(rectangle),
To = p.X,
Duration = new Duration(TimeSpan.FromMilliseconds(500))
};
Storyboard.SetTarget(xAnimation, rectangle);
Storyboard.SetTargetProperty(xAnimation, new PropertyPath("(Canvas.Left)"));
storyboard.Children.Add(xAnimation); DoubleAnimation yAnimation = new DoubleAnimation()
{
From = Canvas.GetTop(rectangle),
To = p.Y,
Duration=new Duration(TimeSpan.FromMilliseconds(500))
};
Storyboard.SetTarget(yAnimation, rectangle);
Storyboard.SetTargetProperty(yAnimation, new PropertyPath("(Canvas.Top)"));
storyboard.Children.Add(yAnimation);
storyboard.Begin();
} }
}
解决方案 »
- 怎样同时更改数据库和文件的事物,一个失败,全部回滚?
- vs2008中winform应用gridveiw批量更新数据
- VS2010谁出现过这种问题??
- 大家好,我想用DataList实现每绑定5个数据项后加条水平线
- winform程序用管理员权限之后,就无法拖入文件了,求解决!
- WMI 磁盘信息相关
- 如何实现一个排列的算法
- 我翻译的MSDN上的有关数据结构的文章(An Extensive Examination of Data Structures)
- 如何用C#的ADO.NET访问SQL SERVER?急!谢谢!
- 关于DATAGRID和DATAVIEWMANAGER的问题
- 给大家拜年了!谁用过USB拨号器
- 正则表达式的问题
是keydown函数没有触发
请问如何触发keyDown函数
在silverlight中,能获取焦点的控件,才可以有相应的键盘事件,意思是说,需要有能显示光标的地方,才可以出发相应的键盘事件,键盘事件也是可以上溯到最顶级的容器的。
请问是不是这个原因呢