title: Xamarin set BackgroundColor of StackLayout on tapped
body: <pre><code>StackLayout sl1 = this.FindByName&lt;StackLayout&gt;("myStackLayout");
  sl1.GestureRecognizers.Add(
      new TapGestureRecognizer() {
          Command = new Command(() =&gt; { 
  
                Task.Run(() =&gt; { 
  
                   // When this line hits, background is set...  
                   sl1.BackgroundColor = Color.FromHex("CCCCCC");
  
  
  
                   //this.Refresh(); &lt;&lt; force Refresh UI function or something????
  
                   Task.Delay(400);
  
                   // When this line hits, background is reset...  
                   sl1.BackgroundColor = Color.FromHex("EEEEEE");
  
                });
  
  
          }) 
     });
  </code></pre>
  
  <p>The above code is working as expected when i debug this code line by line. </p>
  
  <p>However, when i run this code without debugging, the UI will not update the BackgroundColor.</p>
  
  <p>Then when i try to debug to see what is happening, it seems to work.</p>
  
  <ul>
  <li>Why is this not working without debugging?</li>
  <li>Is there a function that forces the UI to update?</li>
  <li>Any other ideas to get the same effect?</li>
  </ul>
  
  <p>EDIT:</p>
  
  <p>The first time it also works.</p>
  
  <p><strong>EDIT 2 (Solution)</strong>:</p>
  
  <p>Using a combination of the two answers i got it to work using the following code:</p>
  
  <pre><code>        StackLayout sl1 = this.FindByName&lt;StackLayout&gt;("myStackLayout");
          sl1.GestureRecognizers.Add(
          new TapGestureRecognizer()
          {
              Command = new Command(async () =&gt;
              {
                  sl1.BackgroundColor = Color.FromHex("#CCCCCC");
  
                  await Task.Run(async () =&gt; { 
                      await Task.Delay(100);
                      Device.BeginInvokeOnMainThread(() =&gt; { 
                          sl1.BackgroundColor = Color.FromHex("#EEEEEE");
                      });
                  });
  
              })
          });
  </code></pre>