首页 文章

在另一个页面视图中获取Frame的元素

提问于
浏览
0

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

<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>

现在,我想在这种情况下使用dashboard.js来定位这个Frame的元素(ico4) . 我想要的就是选择它并为此添加css类 . 但没有什么对我有用 . 我试过这个但没有成功:

dashboard.js:

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

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

编辑:我试过你的路径@Nick Iliev,但对我来说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...

console.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>

现在home-page.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;

1 回答

  • 1

    tns-core-modules 中有 no getElementById 但是 a page method 被称为 getViewById . 要使用它,您需要通过当前帧参考获取页面引用 .

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

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

相关问题