0

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

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);
    }
4

2 に答える 2

1

Windows ストア アプリを開発している場合、ボタン クリック イベントは次のようになります。

using Windows.UI.Core;

private async void Button_Click_1(object sender, RoutedEventArgs e)
{
    while (true)
    {
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => MoveRect());
        Sleep(100);
    }
}
于 2013-04-10T12:35:13.773 に答える