rab*_*ana 26 android xamarin.android xamarin
我刚刚开始使用VS2012使用Xamarin创建一个简单的Android应用程序.我知道有一种资源只用于字符串.在我的资源文件夹中,我有一个像这样的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>
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我想使用这些资源的值,如:
string recordTable = Resource.String.RecordsTable; //error, data type incompatibility
Run Code Online (Sandbox Code Playgroud)
我知道 Resource.String.<key>返回一个整数,所以我不能使用上面的代码.我希望recordTable变量的值为records.
有没有办法可以将这些资源字符串的值用于我的代码的字符串变量?
ρяσ*_*я K 36
尝试使用Resources.GetString从字符串Resources获取字符串
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);
Run Code Online (Sandbox Code Playgroud)
Tie*_* T. 13
值得一提的是,你并不需要创建一个实例Resources来访问资源表.这同样有效:
using Android.App;
public class MainActivity : Activity
{
void SomeMethod()
{
string str = GetString(Resource.String.your_resource_id);
}
}
Run Code Online (Sandbox Code Playgroud)
GetString(),以这种方式使用,是在抽象Context类上定义的方法.您也可以使用此版本:
using Android.App;
public class MainActivity : Activity
{
void SomeMethod()
{
string str = Resources.GetString(Resource.String.your_resource_id);
}
}
Run Code Online (Sandbox Code Playgroud)
Resources,以这种方式使用,是类的只读属性ContextWrapper,它Activity继承自ContextThemeWrapper类.
| 归档时间: |
|
| 查看次数: |
23501 次 |
| 最近记录: |