如何在nativescript中使用本机android小部件类型

如何在nativescript中使用本机android小部件类型

本文介绍了如何在nativescript中使用本机android小部件类型定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过添加tns-platform-declarations来安装平台定义:^ 2.5.2然而,当我使用 tns-platform-declarations / android.d.ts 我无法访问API的某些部分。例如,我无法访问 android.support.design.widget.Snackbar

I installed the platform definitions by adding "tns-platform-declarations": "^2.5.2"However, when I use the definitions in tns-platform-declarations/android.d.ts I can't access certain parts of the API. For instance, I can't access android.support.design.widget.Snackbar

  private snackbar: android.support.design.widget.Snackbar;

因为'android.support'没有导出的成员'design'我缺少什么?当我去代码完成时,我只得到'android.v4'而v4不包含设计模块。

because 'android.support' has no exported member 'design' what am I missing? When I go to code complete, I get only 'android.v4' and v4 does not contain a "design" module.

我还尝试直接使用'app'对象导入,如有人建议:

I also tried to import directly using the 'app' object as somebody suggested:

  import * as app from 'application';
  private snackbar = app.android.support.design.widget.Snackbar;
  // error: 'application' has no member 'android'

通过以上方式,我得到代码完成,但在 android 和tsc下的红色波浪形将无法编译。

With the above, I get code completion, but a red squiggly under android and tsc won't compile.

我找到的唯一替代方案与这不是一个真正的解决方案,因为我也可以这样作弊:

The only alternative I have found, is similar to this solution which isn't really a solution, as I can cheat like this, too:

declare var android:any;

如何实际使用TS定义进行编译并获得代码完成?

推荐答案

有一个用于android sdk的实验性类型定义生成工具,以及项目中使用的android支持库。使用 - androidTypings 标志进行构建/运行,以便为您生成d.ts'。

There's an experimental type definitions generating tool for the android sdk, and android support libraries used in the project. Do a build/run with the --androidTypings flag to have d.ts' generated for you.

这些应该包含在平台声明中找不到的类,因为它们是根据项目中使用的版本生成的。

Those should contain the classes that you can't find in the platform-declarations, as they are generated against the versions used in your project.

tns build android --androidTypings

这篇关于如何在nativescript中使用本机android小部件类型定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:15