如何完全以编程方式初始化NativeScript应用程序(无XML)?
这是我到目前为止所拥有的。背景变为绿色(页面的颜色),但我希望一个紫色的ContentView,里面有一些文本来填充页面 我还缺什么吗如何完全以编程方式初始化NativeScript应用程序(无XML)?,nativescript,Nativescript,这是我到目前为止所拥有的。背景变为绿色(页面的颜色),但我希望一个紫色的ContentView,里面有一些文本来填充页面 我还缺什么吗 从“tns核心模块/应用程序”导入{on,run,launchEvent}; 从“tns核心模块/ui/Frame/Frame”导入{Frame}; 从“tns核心模块/ui/content view/content view”导入{ContentView}; 从“tns核心模块/ui/text-base/text-base”导入{TextBase}; 从“tn
从“tns核心模块/应用程序”导入{on,run,launchEvent};
从“tns核心模块/ui/Frame/Frame”导入{Frame};
从“tns核心模块/ui/content view/content view”导入{ContentView};
从“tns核心模块/ui/text-base/text-base”导入{TextBase};
从“tns核心模块/ui/Page/Page”导入{Page};
打开(launchEvent,(数据)=>{
常量帧=新帧();
常量页面=新页面();
page.backgroundColor=“绿色”;
const contentView=new contentView();
const textBase=new textBase();
contentView.height=100;
contentView.width=100;
contentView.backgroundColor=“紫色”;
textBase.text=“你好,世界!”;
contentView.\u addView(textBase);
page.bindingContext=contentView;
frame.navigate({create:()=>page});
data.root=page;//顺便问一下,这应该是框架还是页面?
});
run();
我想你不能让run
空着,因为它期待一个条目来启动应用程序。从{NS}
您可以使用此文件执行应用程序级初始化,但
该文件的主要用途是将控制权传递给应用程序的根用户
单元为此,需要调用application.run()方法并
传递带有所需moduleName的NavigationEntry作为到
相对于您的/app文件夹的根模块
如果您在“tns核心模块/应用程序”中查找运行代码
及
您几乎已步入正轨,只需对代码稍加修改即可
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;
}
});
您不必等待启动事件,您可以在run
方法本身中设置根帧
在代码中,您创建了框架,但从未将其添加到根UI元素或将框架本身标记为根元素
建议使用.content
为ContentView
/页面添加子元素,因为它们最初设计为仅包含一个子元素
使用TextField
/TextView
输入文本,TextBase
只是一个基类
在我看来,你似乎试图把事情复杂化。只需实现createPage
method-,就可以用代码替换XML
我刚刚修改了默认的NS+TypeScript操场模板,使其在没有XML的情况下运行
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);
}
}
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;
}
});