如何设置TextView的文本?

How to set the text of a TextView?

我正在尝试了解 Xamarin Android,但我有一些疑问。

我有一个布局,我的 .xaml 文件和我的 .cs 文件(我不确定这是否可以像在 WPF 应用程序中一样考虑我的视图模型)。

在我的布局中我有这个:

<TextView
    android:text="txtDireccionServicio"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtDireccionServicio" />

但是在文本中我有一个警告告诉我这是一个硬编码值,我应该使用 @string 资源。

好吧,我的 .cs 文件中有这个 属性:

private string txtDireccionServicio = "Prueba";

但我不知道如何在我的布局中使用这个 属性。我试过这个:

android:text="@txtDireccionServicio"

但它显示“@txtDireccionServicio”。但在这种情况下,布局不会警告它是硬编码值。

所以我的问题是,如何将 TextView 的文本 属性 与我的 .cs 文件中的字符串 属性 绑定?

谢谢。

Android 默认不支持 Data-Binding。所以我们需要使用 Element.Property=xxx

来设置文本

在你的activity

TextView tx = FindViewById<TextView>(Resource.Id.txtDireccionServicio);
tx.Text = "xxx";

注:

如果我们在运行时更改文本的值,我们需要调用行 tx.Text = "xxx";