I have an event that runs when a button is pressed:

private void AnswerPressAction(object sender, RoutedEventArgs e) { Button button = (Button)sender; Button validButton = null; string buttonName = button.Name; button.Background = new SolidColorBrush(Colors.Yellow); System.Threading.Thread.Sleep(1000); int presedAnswer = 0; switch (buttonName) { case "buttonAnswer1": presedAnswer = 1; break; case "buttonAnswer2": presedAnswer = 2; break; case "buttonAnswer3": presedAnswer = 3; break; case "buttonAnswer4": presedAnswer = 4; break; } switch (_valid_answer) { case 1: validButton = buttonAnswer1; break; case 2: validButton = buttonAnswer2; break; case 3: validButton = buttonAnswer3; break; case 4: validButton = buttonAnswer4; break; } if (presedAnswer != 0 && presedAnswer == _valid_answer) { button.Background = new SolidColorBrush(Colors.Green); System.Threading.Thread.Sleep(1000); _gameLevel++; CreateQuestion(_gameLevel); } else { button.Background = new SolidColorBrush(Colors.Red); validButton.Background = new SolidColorBrush(Colors.Green); System.Threading.Thread.Sleep(1000); MainPage mp = new MainPage(); NavigationService.Navigate(new Uri("/MainPage.xaml?show_error=true", UriKind.Relative)); _gameLevel = 0; } } 

The problem is that I cannot set the button color to the desired one at the event, because while the event is running, the button is "pressed", and I need to manipulate the button during the method.

Can you please tell me if there is a way to transfer control to another method, so that the button is no longer pressed, i.e. at the end of the event.

  • you need to play with the button states, if you want to repaint it already pressed, then in the event repaint the state Normal - Dmitry
  • In the handler, create a separate Thread or Task. in it, if you need to do something with the button and other UIs, then use Dispatcher.Invoke (...) - Stack

2 answers 2

Button - a visual element. Control her behavior through xaml triggers .

I can provide a more detailed sample code xaml, if you describe your situation.

  • one
    The only adequate answer. All if only in the code-behind to write. - Make Makeluv

Your problem is solved using asynchronous programming using async / await. Your code quite simply corresponds to the following:

 private async void AnswerPressAction(object sender, RoutedEventArgs e) { Button button = (Button)sender; Button validButton = null; string buttonName = button.Name; button.Background = new SolidColorBrush(Colors.Yellow); await Task.Delay(1000); int presedAnswer = 0; switch (buttonName) { case "buttonAnswer1": presedAnswer = 1; break; case "buttonAnswer2": presedAnswer = 2; break; case "buttonAnswer3": presedAnswer = 3; break; case "buttonAnswer4": presedAnswer = 4; break; } switch (_valid_answer) { case 1: validButton = buttonAnswer1; break; case 2: validButton = buttonAnswer2; break; case 3: validButton = buttonAnswer3; break; case 4: validButton = buttonAnswer4; break; } if (presedAnswer != 0 && presedAnswer == _valid_answer) { button.Background = new SolidColorBrush(Colors.Green); await Task.Delay(1000); _gameLevel++; CreateQuestion(_gameLevel); } else { button.Background = new SolidColorBrush(Colors.Red); validButton.Background = new SolidColorBrush(Colors.Green); await Task.Delay(1000); MainPage mp = new MainPage(); NavigationService.Navigate(new Uri("/MainPage.xaml?show_error=true", UriKind.Relative)); _gameLevel = 0; } } 

Thus, you can not only change the visual state of the elements, but also call other asynchronous methods.

You can read about asynchronous programming and how it works here (English) and on Habrahabr