0到20秒之间的随机时间?

时间:2016-03-23 22:01:47

标签: c# visual-studio xaml random

如何创建一些随机时间,例如10秒。所以,当我点击按钮时,我必须等待10秒才能看到新按钮?我的问题是如何创建随机时间?它必须在Xaml

我的代码:

public void randomButton()
        {            
            newBtn.Content = "A New Button";
            Random myRandom = new Random();
            int rand1 = myRandom.Next(0, 400);
            int rand2 = myRandom.Next(0, 400);
            panelButton.Children.Add(newBtn);

            Canvas.SetLeft(newBtn, rand1);
            Canvas.SetTop(newBtn, rand2);

            iRandom = row.Next(-350, 350);
            newBtn.Click += newBtn_Click;
            newBtn.Margin = new Thickness(iRandom, iRandom, 0, 0);
        }

        private void newBtn_Click(object sender, RoutedEventArgs e)
        {
            *// here i want the random time to happen!*
            iRandom = row.Next(-350, 350);
            newBtn.Margin = new Thickness(iRandom, iRandom, 0, 0);
        }

请帮帮我,我真的很想知道!抱歉,我的英文不好!

1 个答案:

答案 0 :(得分:0)

private async void newBtn_Click(object sender, RoutedEventArgs e)
{
    // here i want the random time to happen!
    await Task.Delay(5000); // 5 sec, make it random

    iRandom = row.Next(-350, 350);
    newBtn.Margin = new Thickness(iRandom, iRandom, 0, 0);
}