如何在不调用navigation()的情况下更改整个视图?

时间:2019-02-03 13:51:24

标签: nativescript

  1. 我需要动态地将整个view更改为另一个(例如,根布局应更改为DockLayout),但不使用Frame.navigate()方法。 怎么办?

  2. 其他问题与View.load()方法有关。这种方法有哪些用例?

const view = parse(`<StackLayout><Button tap="{{ onButtonTap }}" text="Tap me!"/></StackLayout>`);

class ViewModel extends Observable {

    public onButtonTap(args: EventData) {
        // need to change whole view to another here (for example, root layout should be DockLayout now)
        // but without using Frame.navigate() method
        // How do this?
    }
}

app.run({
    create: () => {
        view.bindingContext = new ViewModel();
        return view;
    }
});

1 个答案:

答案 0 :(得分:0)

您可以使用builder

const builder = require('tns-core-modules/ui/builder');

const view = builder.parse(`<StackLayout><Button tap="{{ onButtonTap }}" text="Tap me!"/></StackLayout>`);
view.bindingContext = myBindingContext;

// If you want to replace everything in Page
page.content = view;

Or

// If you want to add it to a parent layout
parentStackLayout.addChild(view);

编辑::如果要替换根视图本身,则必须在_resetRootView模块上使用application方法。

const create = () => {
    const view = builder.parse(`<StackLayout><Button tap="{{ onButtonTap }}" text="Tap me!"/></StackLayout>`);
    view.bindingContext = myBindingContext;
    return view;
};
application._resetRootView({
    create: create
});