【问题标题】:Retrieve string from Strings.xml in C#从 C# 中的 Strings.xml 中检索字符串
【发布时间】:2015-11-25 12:02:24
【问题描述】:

我目前正在用 C# 编写一本用于 Android 的交互式书籍(现在我正在编写它的测试版本)。

我将 strings 存储在文件 Strings.xml (字符串的默认文件)中。

Main.axml 文件中,我创建了一个 TextView:

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

这只是包含存储故事的字符串的基本内容。

Strings.xml 文件中有一个测试字符串:

<string name="storytest1">This is where a piece of the story will go, bla bla, once 
upon a time there was our Hero who did this and that...
</string>

所以在这个 TextView txttest1 我想加载这个字符串。 在我的 MainActivity.cs 文件中,我这样做了:

namespace InteractiveBookTest
{
[Activity (Label = "InteractiveBookTest", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
    private TextView mTextView;

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

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        string customstring = "Here I should retrieve storytest1";

        mTextView = FindViewById<TextView> (Resource.Id.txttest1);
        mTextView.Text = customstring;
    }
}
}

我该怎么做?

【问题讨论】:

  • 参考这个Link
  • 我不知道哪个指令包含 getResources 但我似乎找不到它。当前上下文中不存在名称“getResources”。
  • 哦,我发现 R.string.stringname 可以在 Java 中找到,但我在 C# 中需要这个......或者类似的东西,我对此一无所知。跨度>

标签: c# android xml string


【解决方案1】:

我终于找到了解决办法。

string customstring = GetString (Resource.String.storytest1);

这行得通。如果没有 GetString 部分,它会给出错误,但使用 GetString 并通过 Resource.String.stringname 引用(如果我得到正确的 Resource.String 是指 Resources/values 文件夹中的 Strings.xml)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-14
    • 2019-02-17
    • 2020-10-30
    • 2012-09-20
    • 2018-05-22
    • 2012-02-13
    • 2015-09-25
    • 1970-01-01
    相关资源
    最近更新 更多