我想通过按下键来移动我的矩形,但目前我收到的错误是:
发生了'System.ArgumentException'类型的未处理异常 WindowsBase.dll中
其他信息:'自动,自动,0,0'不是有效值 物业'保证金'。
我的代码如下:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
double x = Canvas.GetLeft(rect);
double y = Canvas.GetTop(rect);
if (e.Key == Key.D)
{
rect.Margin = new Thickness(x+5, y, 0, 0);
}
else if (e.Key == Key.A)
{
rect.Margin = new Thickness(x-5, y, 0, 0);
}
Thread.Sleep(100);
}
答案 0 :(得分:0)
试
private void Window_KeyDown(object sender, KeyEventArgs e)
{
double x = rect.Margin.Left;
double y = rect.Margin.Top;
if (e.Key == Key.D)
{
rect.Margin = new Thickness(x+5, y, 0, 0);
}
else if (e.Key == Key.A)
{
rect.Margin = new Thickness(x-5, y, 0, 0);
}
Thread.Sleep(100);
}
}