【问题标题】:Xamarin Android UI with Timer isn't reacting to changes带有计时器的 Xamarin Android UI 对更改没有反应
【发布时间】:2016-01-04 21:33:48
【问题描述】:

我正在使用 Xamarin Studio,我构建的应用程序应该轮流切换左右列(如警灯)。为此,我使用了计时器和断点,我可以看到代码运行ColorFlip 并且正在设置值,但是我看不到模拟器 UI 中的任何变化。

Main.xml

<?xml version="1.0" encoding="utf-8"?>       
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableLayout
        android:id="@+id/LightSignal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:layout_marginTop="23dp"
        android:stretchColumns="*" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1" >

            <TextView
                android:id="@+id/Left"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:paddingLeft="8dp"
                android:textColor="#FFF000"
                android:background = "#000000"
                android:layout_column="0"
                />

            <TextView
                android:id="@+id/Right"
                android:paddingLeft="4dp"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:textColor="#FFF000"
                android:background = "#000000"
                android:layout_column="1"
                />
        </TableRow>
    </TableLayout>

</LinearLayout>

MainActivity.cs

using System;

using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using Android.Graphics;

namespace Core.Droid
{
    [Activity (Label = "Core.Droid", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {

        bool IsRed; 
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            global::Xamarin.Forms.Forms.Init (this, bundle);
            var app = new App ();
            LoadApplication(app);
            this.SetContentView(Resource.Layout.Main);

            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 1000; 
            timer.Elapsed += ColorFlip;
            timer.Enabled = true;

        }

        private void ColorFlip(object sender, System.Timers.ElapsedEventArgs e)
        {
            var left = FindViewById<TextView> (Resource.Id.Left);
            var right = FindViewById<TextView> (Resource.Id.Right);

            if (IsRed) {
                left.SetBackgroundColor (Color.Black);
                right.SetBackgroundColor (Color.Blue);
                IsRed = false;
            }else {
                left.SetBackgroundColor (Color.Red);
                right.SetBackgroundColor (Color.Black);
                IsRed = true;
            }
        }
    }
}

我假设我需要在更改颜色后强制更新 UI,我该如何解决这个问题?

【问题讨论】:

    标签: c# android timer xamarin xamarin-studio


    【解决方案1】:

    使用RunOnUiThread:

    private void ColorFlip(object sender, System.Timers.ElapsedEventArgs e)
    {
        RunOnUiThread (() => {
            var left = FindViewById<TextView> (Resource.Id.Left);
            var right = FindViewById<TextView> (Resource.Id.Right);
    
            if (IsRed) {
                left.SetBackgroundColor (Color.Black);
                right.SetBackgroundColor (Color.Blue);
                IsRed = false;
            } else {
                left.SetBackgroundColor (Color.Red);
                right.SetBackgroundColor (Color.Black);
                IsRed = true;
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-14
      • 2020-08-30
      • 2018-12-23
      • 2021-02-01
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多