2015-10-16 1 views
3

Je développe une application Xamarin.Android qui référence une bibliothèque de classes portable avec viewmodels. MvvmCross est utilisé. J'ai besoin d'une minuterie qui met à jour l'interface utilisateur chaque fois qu'elle "cochée". Je n'arrive pas à l'obtenir pour mettre à jour l'interface utilisateur. Il exécute la méthode Tick chaque seconde comme confirmé en utilisant le débogueur. J'ai besoin d'utiliser la méthode RunOnUiThread mais je ne sais pas comment l'implémenter dans Xamarin. Un exemple de code permettant de mettre à jour le thread de l'interface utilisateur serait apprécié.Xamarin.Android Minuterie pour mettre à jour l'interface utilisateur - 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(); 
     } 
    } 
} 

l'activité:

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

la mise en page:

<?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> 

ViewModel:

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

Répondre

5

Vous dites que la méthode Tick est appelée régulièrement. Donc, votre seul problème est de mettre à jour l'interface utilisateur. Cela peut être fait avec RunOnUIThread dans la méthode Tick:

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