--> -->

skimemo


skimemo - 日記/2019-04-19/NativeScriptのModal View内で画面遷移する際にcontextを渡す のバックアップ(No.3)


_ NativeScriptのModal View内で画面遷移する際にcontextを渡す

NativeScriptのModal View内で画面遷移する場合、Frameを作ってその中にModal Viewを展開する必要がありますが、公式ドキュメント通りだとcontextと渡りません。

_ 公式ドキュメント方式

main-page.js

  1
  2
  3
const mainpage = args.object.page;
const context = "some context";
mainpage.showModal("modal-root", context, () => {}, true);    // 全画面でmodal-rootを開く


modal-root.xml

  1
<Frame defaultPage="first-modal-view-page"> 


first-modal-view-page.xml

  1
  2
  3
  4
  5
  6
<Page backgroundColor="green" navigatingTo="onNavigatingTo">
    <StackLayout backgroundColor="lightGreen">
        <Button text="Navigate" tap="onNavigate"/>
        <Button text="Close Modal" tap="onCloseModal"/>
    </StackLayout>
</Page> 


first-modal-view-page.js

  1
  2
  3
  4
function onNavigatingTo(args) {
    console.log(args.context);    // undefined
}
exports.onNavigate = onNavigate;

_ 解決方法

そこで、参考ページの通り、frameを作ってあげてその中のshownModallyイベントからさらにcontextを渡すようにします。

main-page.js

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
const Frame = require("tns-core-modules/ui/frame").Frame;
 
function onTap(args){
    const frame = new Frame();
    frame.on("shownModally",(args)=>{
        const component = args.object;
        if(!component.isLayoutValid){     // 一度backgroundにして再表示した場合は処理しない
            frame.navigate({ 
                moduleName: "first-modal-view-page",
                context: args.context     // showModal()のcontextを次のページへ渡す
            });
        }
    });
    const page = args.object.page;
    const context = "some context";
    page.showModal(frame, context, () => {}, true);
}

modal-root.xmlは不要です。
これでModal Viewの中でnavigateできますし、ActionBarも効くようになりました。

_ 2019/4/23追記

参考ページのままだと、HOMEボタンで一旦アプリを閉じた後再表示させた再に再度Navigateしてしまい、backボタンを押しても再度同じ画面が表示される(二重に画面遷移している)事が分かりました。このため、再表示時はnavigateしないよう、7行目のif文を追加しました。
但し、このisLayoutValidの役割についてはいまいちはっきりしていません。それぞれの操作をした際のプロパティの違いを元に実装していますので、場合によっては期待通り動いてくれない事があるかもしれません。

Category: [NativeScript] - 06:01:53