【发布时间】:2012-02-21 05:53:11
【问题描述】:
我想通过单击按钮来移动圆圈。例如;
(0,0) 的位置有一个圆圈,我想通过单击 X+ 按钮来移动它。这将在不停止的情况下将 X 一一增加,并在 X 位置上循环移动。然后当我点击 Y+ 按钮时,它只会增加 20 倍 Y 并且圆圈也开始在 Y 轴上移动。
我有一个代码,但我无法动态移动它。它移动到预定义的位置。
XAML:
<Window x:Class="circle_animation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Closed="Window_Closed" >
<Canvas>
<Ellipse Width="10" Height="10" Canvas.Left="0" Canvas.Top="0" Fill="Black" x:Name="el" />
<Button Canvas.Left="255" Canvas.Top="266" Content="Move On X" Height="23" Width="75" Click="Button_Click" />
<Button Canvas.Left="139" Canvas.Top="272" Content="Move On Y+" Height="23" Name="button1" Width="75" Click="Button2_Click" />
</Canvas>
代码:
public int X;
public int Y;
public bool inside = true;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (inside)
{
DoubleAnimation animatex = new DoubleAnimation();
animatex.To = X;
el.BeginAnimation(Canvas.LeftProperty, animatex);
DoubleAnimation animatey = new DoubleAnimation();
animatey.To = Y;
el.BeginAnimation(Canvas.TopProperty, animatey);
}
}
public void Button_Click(object sender, RoutedEventArgs e)
{
if (inside)
{
X++;
}
}
public void Button2_Click(object sender, RoutedEventArgs e)
{
Y = Y + 20;
}
这是我动态移动它的想法,但它不起作用。你能帮我吗 ?我哪里做错了?
【问题讨论】:
-
您是否期望圆在不断移动并且通过改变 X 和 Y 来神奇地改变它的“速度矢量”?
-
是的,正是我想这样做。
标签: c# wpf animation drawing geometry