title: Xamarin.Android Timer to update the UI - runOnUiThread
body: <p>I'm developing a Xamarin.Android app that references a portable class library with viewmodels. MvvmCross is being used. I need a Timer that updates the UI every time it "ticks". I just can't seem to get it to update the UI. It is performing the Tick method every second as confirmed by using the debugger. I need to use the RunOnUiThread method but I'm just unsure how to implement it in Xamarin. A code example causing tick to update the UI thread would be appreciated.</p>
  
  <p>Ticker.cs:</p>
  
  <pre><code>using System;
  using Pong.Core.Models;
  using Pong.Core.ViewModels;
  using System.Threading;
  
  namespace Pong.Droid
  {
      public class Ticker
      {
          private readonly Timer _dispatcherTimer;
  
          private readonly GamePlayViewModel _viewModel;
  
  
          public Ticker(GamePlayViewModel viewModel)
          {
              _viewModel = viewModel;
              TimerCallback timerDelegate = new TimerCallback (Tick);
              _dispatcherTimer = new Timer (timerDelegate, null, 0, 1000);
          }
  
  
  
          public void Tick(object state)
          {
              _viewModel.Number++;
  
  
              //_viewModel.UpdateBall();
              //_viewModel.UpdatePaddle1();
          }
      }
  }
  </code></pre>
  
  <p>the activity:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  using Android.App;
  using Android.Content;
  using Android.OS;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  using Pong.Core.ViewModels;
  using Cirrious.MvvmCross.Droid.Views;
  using Android.Content.PM;
  
  namespace Pong.Droid
  {
      [Activity (Label = "GamePlayView", ScreenOrientation = ScreenOrientation.Landscape)]            
      public class GamePlayView : MvxActivity
      {
          private GamePlayViewModel _vm;
          protected override void OnCreate (Bundle bundle)
          {
              base.OnCreate (bundle);
              SetContentView (Resource.Layout.GamePlayView);
  
              _vm = new GamePlayViewModel();
              DataContext = _vm;
              var ticker = new Ticker(_vm);
          }
      }
  }
  </code></pre>
  
  <p>the layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:local="http://schemas.android.com/apk/res-auto"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
      &lt;EditText
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="40dp"/&gt;
      &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="40dp"
          local:MvxBind="Text Number" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>the viewmodel:</p>
  
  <pre><code>using Pong.Core.Models;
  using System.Diagnostics;
  using Cirrious.MvvmCross.ViewModels;
  
  
  namespace Pong.Core.ViewModels
  {
      public class GamePlayViewModel : MvxViewModel
      {
          protected Paddle Paddle1;
          private Paddle _paddle2; // Not yet implemented
          protected StandardBall StandardBall;
          public int Number { get; set; }
  
          public GamePlayViewModel()
          {
              Paddle1 = new Paddle();
              StandardBall = new StandardBall();
              Number = 1;
          }
  
          public void UpdatePaddle1()
          {
              switch (Paddle1.DetectWallCollision())
              {
                  case "upper":
                      Paddle1.UpperWallHit();
                      break;
                  case "lower":
                      Paddle1.LowerWallHit();
                      break;
                  case "none":
                      Paddle1.MoveOneFrame();
                      break;
              }
          }
  
          public void UpdateBall()
          {
              if (StandardBall.DetectWallCollision()) StandardBall.HandleWallCollision();
              StandardBall.MoveOneFrame();
          }
  
          public void SetPaddleDirection(string direction)
          {
              Paddle1.SetDirection(direction);
          }
  
          public void StopPaddle()
          {
              Paddle1.StopMoving();
          }
      }
  }
  </code></pre>