如何完全以编程方式初始化NativeScript应用程序(不使用XML)?

时间:2019-03-25 23:11:08

标签: nativescript

这是我到目前为止所拥有的。背景变成绿色(页面的颜色),但是我希望紫色的ContentView里面也填充一些文本。

还有什么我想念的吗?

import { on, run, launchEvent } from "tns-core-modules/application";
import { Frame } from "tns-core-modules/ui/frame/frame";
import { ContentView } from "tns-core-modules/ui/content-view/content-view";
import { TextBase } from "tns-core-modules/ui/text-base/text-base";
import { Page } from "tns-core-modules/ui/page/page";

on(launchEvent, (data) => {
    const frame = new Frame();
    const page = new Page();
    page.backgroundColor = "green";

    const contentView = new ContentView();
    const textBase = new TextBase();
    contentView.height = 100;
    contentView.width = 100;
    contentView.backgroundColor = "purple";
    textBase.text = "Hello, world!";
    contentView._addView(textBase);
    page.bindingContext = contentView;

    frame.navigate({ create: () => page });

    data.root = page; // Incidentally, should this be the frame or the page?
});

run();

3 个答案:

答案 0 :(得分:2)

您几乎步入正轨,您只需要对代码稍作修改即可。

import { on, run, launchEvent } from 'tns-core-modules/application';
import { Frame } from 'tns-core-modules/ui/frame/frame';
import { ContentView } from 'tns-core-modules/ui/content-view/content-view';
import { TextField } from 'tns-core-modules/ui/text-field';
import { Page } from 'tns-core-modules/ui/page/page';

run({
    create: () => {
        const frame = new Frame();
        frame.navigate({
            create: () => {
                const page = new Page();
                page.backgroundColor = "green";

                const contentView = new ContentView();

                const textField = new TextField();
                contentView.height = 100;
                contentView.width = 100;
                contentView.backgroundColor = "purple";
                textField.text = "Hello, world!";

                contentView.content = textField;
                page.content = contentView;

                return page;
            }
        });
        return frame;
    }
});
  1. 您不必等待启动事件,可以在run方法本身中设置根框架。
  2. 在代码中,您正在创建框架,但从未将其添加到根UI元素或将框架本身标记为根元素
  3. 建议使用.contentContentView / Page添加子项,因为它们最初设计为仅容纳一个子元素。
  4. 使用TextField / TextView作为输入文本,TextBase只是基类。

答案 1 :(得分:1)

在我看来,您尝试过分复杂。您只需执行createPage方法-Create a page via code就可以用代码替换XML。

我刚刚修改了默认的 NS + TypeScript 游乐场模板,使其无需XML-NS + TypeScript template without XML就可以运行。

答案 2 :(得分:0)

我认为您不能将run留空,因为它期望启动该应用程序。来自{NS} website

  

您可以使用此文件执行应用程序级的初始化,但是   文件的主要目的是将控制权传递到应用程序的根目录   模块。为此,您需要调用application.run()方法并   传递带有所需moduleName的NavigationEntry的路径   相对于/ app文件夹的根模块。

如果您在“ tns-core-modules /应用程序”中寻找run代码

function run(entry) {
    createRootFrame.value = false;
    start(entry);
}
exports.run = run;

function start(entry) {
    if (started) {
        throw new Error("Application is already started.");
    }
    started = true;
    mainEntry = typeof entry === "string" ? { moduleName: entry } : entry;
    if (!androidApp.nativeApp) {
        var nativeApp = getNativeApplication();
        androidApp.init(nativeApp);
    }
}