简体   繁体   中英

Xamarin.Android Timer to update the UI - runOnUiThread

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.

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

the activity:

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

the layout:

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

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

You say the Tick method is called regularly. So your only problem is to update the UI. This can be done with RunOnUIThread in the Tick method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM