有人知道如何检测Nativescript的方向变化吗? [英] Does anybody know how to detect orientation change for Nativescript?

查看:107
本文介绍了有人知道如何检测Nativescript的方向变化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为屏幕创建了两个xml文件。一个名为login-page.port.xml,另一个名为login-page.land.xaml。

I have created two xml files for a screen. one named "login-page.port.xml" and the other one is "login-page.land.xaml".

有没有办法以编程方式检测方向变化在申请中?

Is there a way to programmatically detect orientation change within the application?

谢谢,
Keks

Thanks, Keks

推荐答案

是的,有一种方法可以检测应用程序中的方向变化。最简单的方法是在您想要获得方向的页面中;将已加载和已卸载的事件添加到page.xml文件中:

Yes, there is a way to detect orientation change in the application. The easiest method is in the page you want to get the orientation; add the loaded and unloaded events to your page.xml files:

<Page loaded="pageLoaded" unloaded="pageUnloaded">

在page.js文件中;添加如下代码:

In your page.js file; add code like this:

var application=require('application');
exports.pageLoaded = function() {
  application.on(application.orientationChangedEvent, setOrientation);
};

exports.pageUnloaded = function() {
  application.off(application.orientationChangedEvent, setOrientation);
}

function setOrientation(args) {
   // Will console out the new Orientation
   console.log(args.newValue);
}

几个笔记;
1.你想添加&在进入和退出页面时删除事件监听器;否则,听众是全球性的,即使你在另一个页面上,当方向改变时也会继续发射。
2.您可以为此事件添加多个侦听器,因此,如果您不删除侦听器,则每次重新加载此页面时,您将向该组添加此侦听器的另一个副本,因此它将你添加它的次数很多次。
3.在v1.6.1中的android上,他们是定向射击中的一个错误。 https://github.com/NativeScript/NativeScript/issues/1656 (基本上如果你从横向开始,旋转它将无法检测到它。问题有我已手动应用到我的代码的补丁。)

A couple notes; 1. You want to add & remove the event listener as you enter and exit the page; otherwise the listener is global and will continue to fire when the orientation changes even when you are on another page. 2. You can add multiple listeners to this event, so again if you don't remove the listener, then each time you re-load this page you will add ANOTHER copy of this listener to the group and so it will fire as many times as you have added it. 3. On android in v1.6.1 their is a bug in the orientation firing. https://github.com/NativeScript/NativeScript/issues/1656 (Basically if you start in Landscape, rotate it won't detect it. The issue has the patch that I've applied manually to my code).

现在看看你的实际例子;你是否意识到至少从1.6x版本的NativeScript开始,它只会加载当前设置为哪个方向的XML;但是在旋转时它不会加载另一个方向的xml。所以,如果你在景观;进入屏幕然后旋转它仍将使用Landscape xml文件。

Now Looking at your actual example; are you aware that at least as of version 1.6.x of NativeScript it will only load the XML for which ever orientation it currently is set as; but it will NOT load the other orientation's xml when you rotate. So, if you are in Landscape; enter the screen then rotate it will still be using the Landscape xml file.

现在有了所说的话你可能会认真考虑看 nativescript-orientation 插件,我是这个插件,这个插件简化了处理屏幕方向,并允许你只有一个.xml文件,然后通过css。

Now with all that said you might seriously consider looking at the nativescript-orientation plugin which I am the author of, this plugin simplifies dealing with screen orientation and allows you to only have ONE .xml file and then change things via css.

这篇关于有人知道如何检测Nativescript的方向变化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆