在另一个页面视图上获取框架的元素



我有一些指向仪表板/仪表板/仪表板的框架:

<Page class="page" xmlns="http://www.nativescript.org/tns.xsd" loaded="load">
<GridLayout rows="*,60">
<Frame defaultPage="dashboard/dashboard/dashboard/" id="dashboard"></Frame>
<GridLayout row="1">
<StackLayout verticalAlignment="bottom" row="2" class="bottom-nav">
<GridLayout rows="*" columns="*" height="70">
<Label class="icon4 foot-icon" row="0" col="0" id="ico4"></Label>
</GridLayout>
</StackLayout>
</GridLayout>
</GridLayout>
</Page>

现在,我想使用仪表板.js在这种情况下针对此框架的元素 (ico4(。我想要的只是选择这个并为此添加 css 类。 但没有什么对我有用。我已经尝试过这个但没有成功:

仪表板.js:

var frame = require('ui/frame');
var topmost = frame.getFrameById('dashboard').getElementById('ico4');
var ic = topmost;
console.log(ic);

我的问题是 - 这有可能以任何方式针对这个 ico4 吗? 谢谢。

编辑: 我已经尝试过你@Nick伊利耶夫的道路,但对我来说,ICO是不确定的......

var frame = require('ui/frame');
var myFrame = frame.getFrameById("dashboard");
// Its working the output :Frame<dashboard>@file:///app/dashboard/root.xml:3:5;
console.log(myFrame);
var myPage = myFrame.currentPage;
// It's working too
console.log(myPage);
var ico = myPage.getViewById("ico4");
// Not working - Undefined...

控制台.log(ICO(;

编辑:我添加了简单的游乐场来显示我的问题:https://play.nativescript.org/?template=play-js&id=uVY4Ir

//app-root.xml (frame has ID)
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="load">
<GridLayout rows="*,60">
<Frame defaultPage="home/home-page" id="dashboard"></Frame>
<GridLayout row="1">
<StackLayout verticalAlignment="bottom" row="2" class="bottom-nav">
<GridLayout rows="*" columns="*,*,*,*,*,*" height="70">
<Label id="ico1" text="test"></Label>
</GridLayout>
</StackLayout>
</GridLayout>
</GridLayout>

现在主页.js

var frameModule = require("tns-core-modules/ui/frame");
var HomeViewModel = require("./home-view-model");
var homeViewModel = new HomeViewModel();
function pageLoaded(args) {
var page = args.object;
page.bindingContext = homeViewModel;
var root = frameModule.getFrameById('dashboard');
// this console.log will show frame's info
console.log(root);
// But this below undefined. Why the frame cannot find this element ?
var ico = root.getViewById('ico1');
console.log(ico);
}
exports.pageLoaded = pageLoaded;

tns-core-modules没有getElementById,但有一个名为getViewById页面方法。要使用它,您需要通过当前框架引用获取页面引用。

下面是如何在选定框架中获取当前页面的示例。

const frame = getFrameById("my-frame-id");
const page = frame.currentPage;
let ico = page.getViewById("ico4");

最新更新