【问题标题】:TextView not displaying the string assigned to the Text propertyTextView 不显示分配给 Text 属性的字符串
【发布时间】:2021-12-17 01:37:01
【问题描述】:

我有一个这样定义的 TextView:

<TextView
        android:id="@+id/middleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Click the mail button" />

我得到这样的参考:

    private TextView _textView;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        _textView = FindViewById<TextView>(Resource.Id.middleText);

当点击视图中的按钮时,会运行以下代码:

        if (_accelerometerController.IsStarted)
        {
            _accelerometerController.Stop();
            _timer?.Stop();
            _timer?.Dispose();
            _timer = null;
        }
        else
        {
            _accelerometerController.Start();
            _timer = new Timer(1000);
            _timer.Elapsed += Timer_Elapsed;
            _timer.AutoReset = true;
            _timer.Enabled = true;
        }

当调用 _timer.Elapsed 事件时,将运行以下代码:

    private void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        //MainThread.BeginInvokeOnMainThread(() => _textView.Text = _accelerometerController.Status);
        //_textView.SetText(_accelerometerController.Status, TextView.BufferType.Normal);
        //RunOnUiThread(() => _textView.Text = _accelerometerController.Status);
        _textView.Text = _accelerometerController.Status;
        _textView.Invalidate();
    }

_textView.Text 属性设置正确,但图形大部分时间不会更新以显示新字符串。一旦应用程序选择不更新图形,它就不会恢复,并且文本似乎永远相同,并且在调试时我可以看到 Text 属性设置正确并且其他控件继续工作。 我尝试了多种不同的方式来设置文本,但都产生了相同的结果。

我是 Xamarin 的新手,我不确定项目类型,我为此项目选择了“Android App (Xamarin)”项目模板,但我不确定这是否相同,或包括 Xamarin.Forms。

为什么会这样?我该如何解决?

【问题讨论】:

  • 通过创建 Android App (Xamarin),您正在创建 Xamarin.Android 应用程序,这意味着它是 Xamarin.Native for Android 应用程序,而不是 Xamarin.Forms 应用程序。

标签: c# android xamarin xamarin.android


【解决方案1】:

如果您使用 Xamarin.Android 项目,您可以参考下面的代码。

布局:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/middleText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Click the mail button" />
<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

Activity:由于不知道_accelerometerController是什么,所以直接启动定时器。

  public class Activity8 : Activity
{
    private TextView _textView;
    private Button button;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here

        SetContentView(Resource.Layout.layout8);
        _textView = FindViewById<TextView>(Resource.Id.middleText);
        button = FindViewById<Button>(Resource.Id.button1);

        button.Click += Button_Click;

    }

    private void Button_Click(object sender, EventArgs e)
    {
        Timer _timer = new Timer(1000);
        _timer.Elapsed += Timer_Elapsed;
        _timer.AutoReset = true;
        _timer.Enabled = true;
    }
    private void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        //MainThread.BeginInvokeOnMainThread(() => _textView.Text = _accelerometerController.Status);
        //_textView.SetText(_accelerometerController.Status, TextView.BufferType.Normal);
        //RunOnUiThread(() => _textView.Text = _accelerometerController.Status);
        _textView.Text = "Hello";
        _textView.Invalidate();
    }
}

【讨论】:

    猜你喜欢
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 2021-04-23
    • 2017-04-23
    • 2021-03-11
    • 2021-08-30
    相关资源
    最近更新 更多