How to dynamically add view in page or layout

不问归期 提交于 2020-01-06 07:14:47

问题


I can't figure out how to programatically add a view into a layout or page.

I need to add views at runtime without using static xml declaration since i need to fetch them from an http requested object... . I didn't find useful informations in the docs.

Anyone knows how to do?


回答1:


I think you meant to dynamically add some view / controls to the page rather than to navigate into another page. If so, you just need to add some controls into one of the layouts in your page (only containers [=layouts] can have multiple children.

so, your code (viewmodel/page controller) would look something like:
var layout = page.getViewById("Mycontainer");
// create dynamic content
var label = new Label();
label.text = "dynamic";
// connect to live view
layout.addChild(label)



回答2:


In addition to having a page included inside your app (normal); you download the xml, css, & js to another directory and then navigate to it by then doing something like page.navigate('downloaded/page-name');

you can also do

var factoryFunc = function () {
    var label = new labelModule.Label();
    label.text = "Hello, world!";
    var page = new pagesModule.Page();
    page.content = label;
    return page;
};

topmost.navigate(factoryFunc);

https://docs.nativescript.org/navigation#navigate-with-factory-function




回答3:


You should check out this thread on the {N} forum. The question is about dynamically loading a page and module from a remote server. The (possible) solution is given in this thread.



来源:https://stackoverflow.com/questions/30548991/how-to-dynamically-add-view-in-page-or-layout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!