xamarin android ui,c# – Xamarin.Android Timer更新UI – runOnUiThread

我正在开发一个Xamarin.

Android应用程序,它引用了一个带有viewmodels的可移植类库.正在使用MvvmCross.我需要一个Timer,每次“滴答”时都会更新UI.我似乎无法让它更新UI.正如使用调试器确认的那样,它每秒执行一次Tick方法.我需要使用RunOnUiThread方法,但我不确定如何在Xamarin中实现它.将会感谢导致tick更新UI线程的代码示例.

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

}

}

}

布局:

xmlns:local="http://schemas.android.com/apk/res-auto"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="40dp"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="40dp"

local:MvxBind="Text Number" />

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

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值