我正在研究一个项目,我想设置一个矩形动画来移动我之前设置的一定数量的坐标。
Ex:我的矩形位于(x = 0,y = 0)的位置。我想要点击一个按钮使其在100毫秒的间隔内移动到位置(x = 150,y = 230)。因此,只需单击一下,它将在前100毫秒(10,35)进入第二个100毫秒,依此类推,直到矩形到达最终位置(x = 150,y = 230)...
我正在使用visual studio并融入Visual C#wpf。
这是我在代码隐藏文件中的代码进度:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
var myRect = (Rectangle)this.FindName("myRect");
double x = Canvas.GetLeft(myRect);
double y = Canvas.GetTop(myRect);
Canvas.SetLeft(myRect,x+10);
Canvas.SetTop(myRect,y);
这是在XAML代码中:
<Window x:Class="move.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:move"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Border BorderThickness="1" BorderBrush="Aqua">
<Canvas Name="PointCanvas" Width="500" Height="294" Margin="9,0,6,0">
<Rectangle x:Name="myRect" Fill="#FFF5F4F5" Height="39" Canvas.Left="170" Stroke="Black" Canvas.Top="89" Width="89"/>
</Canvas>
</Border>
<Button Name="Move" Click="Button_Click">Move</Button>
</StackPanel>