如何在NativeScript中访问android的R对象?

时间:2018-07-07 18:37:40

标签: nativescript angular2-nativescript nativescript-angular

我正在尝试使用Angular在NativeScript中访问android的R对象,但没有取得任何成功。说明here表示要这样做:

android.R.color.holo_purple

但是当我尝试从android模块或application模块导入tns-core-modules/platform变量时,我得到一个错误,即{{1}上的R属性}对象未定义。如何获得对android的访问权限?

3 个答案:

答案 0 :(得分:2)

您不需要从android模块导入application变量。默认情况下在运行时自动可用。要消除编译时错误property doesn't exists,只需将名为android的变量声明为any

例如。

declare var android:any;
export class AppComponent{
  let holoPurple=android.R.color.holo_purple;
}

答案 1 :(得分:0)

这里的android是Android SDK的主要名称空间,而不是android模块中的application属性。

有关如何通过TypeScript访问本机SDK或如何使用this StackOverflow answer中的指令,请参见the related documentation article

答案 2 :(得分:0)

无法获得现有的答案以使用最新的本机脚本/ angular。我用以下方法解决了这个问题:

创建了一个文件App_Resources / Android / values / keys.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="key_name">xxxxxx</string>
</resources>

导入这些

import * as app from 'tns-core-modules/application';
import * as utils from 'tns-core-modules/utils/utils';

然后抓取字符串

const context = app.android.context;
const res = context.getResources().getString(utils.ad.resources.getStringId('key_name'));