フォームにボタンがあります。ボタンを押すと、長方形が移動する必要があります。しかし、何も起こりません、なぜですか?将来非同期メソッドを呼び出したいので、ボタンが非同期であることは重要です。
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);
}