我在表格上有一个按钮。通过按下按钮,矩形必须移动。但是什么都没有发生,为什么?对我来说,按钮是异步的很重要,因为我想在未来调用异步方法。
XAML:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Button Content="Start" HorizontalAlignment="Left" Margin="849,152,0,0" VerticalAlignment="Top" Height="45" Width="196" Click="Button_Click_1"/>
<Rectangle x:Name="rect" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="100"/>
</Grid>
C#:
private double step = 5;
private async void Button_Click_1(object sender, RoutedEventArgs e)
{
while (true)
{
MoveRect();
Sleep(100);
}
}
private void MoveRect()
{
rect.Margin = new Thickness(rect.Margin.Left + step, rect.Margin.Top + step, rect.Margin.Right - step, rect.Margin.Bottom - step);
}
static void Sleep(int ms)
{
new System.Threading.ManualResetEvent(false).WaitOne(ms);
}