通过 Telerik App Builder 开发原生应用时 main-page.js 和 main-view.model.js 有什么区别
What is the difference between main-page.js and main-view.model.js when developing native apps through Telerik App Builder
如何通过 main-page.js 更改基于 xml 的文本字段的文本?我使用 this.set("message", getMessage(this.counter));
通过主视图-model.js 更改值。但是当我在 main-page.js 中尝试时,这种情况不起作用。这个怎么做? :-) 我需要对我的主要问题进行解释。提前致谢。
我假设您谈论的是可以找到的 "Hello World" NativeScript 应用程序 here. The difference is that the main-page.js is the "code behind" of the main-page.xml (note that naming convention is important for {N} to make the match) and the main-view-model.js is a separate file which has been assigned as bindingContext
to the Page of the main-page.xml in its navigatingTo event as you can see here:
function onNavigatingTo(args) {
/*
This gets a reference this page’s <Page> UI component. You can
view the API reference of the Page to see what’s available at
https://docs.nativescript.org/api-reference/classes/_ui_page_.page.html
*/
var page = args.object;
/*
A page’s bindingContext is an object that should be used to perform
data binding between XML markup and JavaScript code. Properties
on the bindingContext can be accessed using the {{ }} syntax in XML.
In this example, the {{ message }} and {{ onTap }} bindings are resolved
against the object returned by createViewModel().
You can learn more about data binding in NativeScript at
https://docs.nativescript.org/core-concepts/data-binding.
*/
page.bindingContext = createViewModel();
}
为了在其代码隐藏文件中更改 main-page.xml 的标签 (TextView) 的文本,您可以通过 id (getViewById()
example) 或者直接使用 bindingContext
(你的 'ViewModel'):
var createViewModel = require("./main-view-model").createViewModel;
var viewModel;
function onNavigatingTo(args) {
var page = args.object;
viewModel = = createViewModel();
page.bindingContext = viewModel;
}
// Example with event handler for a 'tap' event of a Button
function onButtonTap(args) {
viewModel.set("message", "New message set via code behind");
}
如何通过 main-page.js 更改基于 xml 的文本字段的文本?我使用 this.set("message", getMessage(this.counter));
通过主视图-model.js 更改值。但是当我在 main-page.js 中尝试时,这种情况不起作用。这个怎么做? :-) 我需要对我的主要问题进行解释。提前致谢。
我假设您谈论的是可以找到的 "Hello World" NativeScript 应用程序 here. The difference is that the main-page.js is the "code behind" of the main-page.xml (note that naming convention is important for {N} to make the match) and the main-view-model.js is a separate file which has been assigned as bindingContext
to the Page of the main-page.xml in its navigatingTo event as you can see here:
function onNavigatingTo(args) {
/*
This gets a reference this page’s <Page> UI component. You can
view the API reference of the Page to see what’s available at
https://docs.nativescript.org/api-reference/classes/_ui_page_.page.html
*/
var page = args.object;
/*
A page’s bindingContext is an object that should be used to perform
data binding between XML markup and JavaScript code. Properties
on the bindingContext can be accessed using the {{ }} syntax in XML.
In this example, the {{ message }} and {{ onTap }} bindings are resolved
against the object returned by createViewModel().
You can learn more about data binding in NativeScript at
https://docs.nativescript.org/core-concepts/data-binding.
*/
page.bindingContext = createViewModel();
}
为了在其代码隐藏文件中更改 main-page.xml 的标签 (TextView) 的文本,您可以通过 id (getViewById()
example) 或者直接使用 bindingContext
(你的 'ViewModel'):
var createViewModel = require("./main-view-model").createViewModel;
var viewModel;
function onNavigatingTo(args) {
var page = args.object;
viewModel = = createViewModel();
page.bindingContext = viewModel;
}
// Example with event handler for a 'tap' event of a Button
function onButtonTap(args) {
viewModel.set("message", "New message set via code behind");
}