2015-10-16 19 views
3

我正在开发一个Xamarin.Android应用程序,该应用程序引用具有viewmodels的可移植类库。 MvvmCross正在使用中。我需要一个定时器,每当它“滴答”时都会更新UI。我似乎无法得到它来更新用户界面。它通过使用调试器确认每秒执行Tick方法。我需要使用RunOnUiThread方法,但我只是不确定如何在Xamarin中实现它。一个代码示例导致滴答更新UI线程将不胜感激。更新UI的Xamarin.Android计时器 - runOnUiThread

Ticker.cs:

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

活性:

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

的布局:

<?xml version="1.0" encoding="utf-8"?> 
<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"> 
    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="40dp"/> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="40dp" 
     local:MvxBind="Text Number" /> 
</LinearLayout> 

的视图模型:

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

回答

5

你说定期调用Tick方法。所以你唯一的问题是更新用户界面。 这可以通过Tick方法中的RunOnUIThread完成:

public void Tick(object state) 
{    
    RunOnUiThread (() => _viewModel.Number++); 
}