(Android Xamarin)获取资源字符串值而不是int

(Android Xamarin)获取资源字符串值而不是int,android,xamarin.android,xamarin,Android,Xamarin.android,Xamarin,我刚刚开始使用VS2012创建一个简单的android应用程序,使用Xamarin。 我知道有一种资源只用于字符串。 在我的资源文件夹中,我有一个xml文件,如下所示: <?xml version="1.0" encoding="utf-8"?> <resources> <string name="RecordsTable">records</string> <string name="ProjectTable">proje

我刚刚开始使用VS2012创建一个简单的android应用程序,使用Xamarin。 我知道有一种资源只用于字符串。 在我的资源文件夹中,我有一个xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="RecordsTable">records</string>
   <string name="ProjectTable">projects</string>
   <string name="ActivitiesTable">activities</string>
</resources>
我知道
Resource.String.
返回一个整数,所以我不能使用上面的代码。 我希望
recordTable
变量的值为
records

有没有一种方法可以将这些资源字符串的值用于代码的字符串变量

int int_recordTable = Resource.String.RecordsTable;

String recordTable = (String.valueOf(int_recordTable)); //no more errors
获取
int
,然后将其更改为
字符串

尝试将其用作从字符串资源获取字符串

Context context = this;
// Get the Resources object from our context
Android.Content.Res.Resources res = context.Resources;
// Get the string resource, like above.
string recordTable = res.GetString(Resource.String.RecordsTable);

值得注意的是,您不需要创建
资源的实例来访问资源表。这同样有效:

using Android.App;

public class MainActivity : Activity
{
    void SomeMethod()
    {
        string str = GetString(Resource.String.your_resource_id);
    }
}
以这种方式使用的
GetString()
,是在抽象
上下文
类上定义的方法。您也可以使用此版本:

using Android.App;

public class MainActivity : Activity
{
    void SomeMethod()
    {
        string str = Resources.GetString(Resource.String.your_resource_id);
    }
}

以这种方式使用的
Resources
ContextWrapper
类上的只读属性,该类的
Activity
继承自
ContextThemeWrapper
类。

如果您不在活动或其他上下文中,则应获取上下文并使用它来获取资源和PackageName,例如:

int resID = context.Resources.GetIdentifier(listEntryContact.DetailImage.ImageName, "drawable", context.PackageName);
imageView.SetImageResource(resID);

只需获取
int
,然后将其转换为
字符串
?对不起,我无法理解您的建议。嗯,我看不到GetString方法。为了能够使用Resources对象,我需要添加Android.Content.Res名称空间。@eaon21:是的,我看到了!谢谢但是,我使用了Resources.System.GetString(id)。。。Resources.GetString不存在:(@eaon21:取决于您调用GetString方法的方式我认为我没有看到Resources.GetString()的原因是因为我使用C#进行编码。我猜您必须使用Resources.System.GetString()相反,在C#中。由于OP使用的是Xamarin,他可能是在.Net中编写代码,而不是在Java中编写代码。此外,我不认为他只是想将int转换为字符串
int resID = context.Resources.GetIdentifier(listEntryContact.DetailImage.ImageName, "drawable", context.PackageName);
imageView.SetImageResource(resID);