gpt4 book ai didi

c# - Xamarin.Android 更新 UI 的定时器 - runOnUiThread

转载 作者:太空狗 更新时间:2023-10-30 01:02:37 25 4
gpt4 key购买 nike

我正在开发一个 Xamarin.Android 应用程序,它引用一个带有 View 模型的可移植类库。正在使用 MvvmCross。我需要一个计时器,它在每次“滴答”时更新 UI。我似乎无法让它更新用户界面。使用调试器确认,它每秒执行一次 Tick 方法。我需要使用 RunOnUiThread 方法,但我不确定如何在 Xamarin 中实现它。我们将不胜感激导致滴答更新 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);
}
}
}

布局:

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

View 模型:

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

最佳答案

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

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

关于c# - Xamarin.Android 更新 UI 的定时器 - runOnUiThread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33161766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com