【发布时间】:2020-04-20 20:38:54
【问题描述】:
我有一个 Ionic / Cordova 应用程序,我(也)为 Windows 10 (UWP) 构建。
在平板电脑(例如 Microsoft Surface)上,我希望能够在调用软键盘时缩小应用程序。
我还没有找到这样做的直接方法,因此作为一种解决方法,我在输入焦点/模糊事件上执行此操作,但是,我至少希望能够确定是否使用软键盘正在使用,所以我在桌面上运行时不会这样做。
这是我的尝试(不起作用)
public static isUsingWindowSoftKeyboard(): boolean {
try {
Utils.logger.info('isUsingWindowSoftKeyboard begin');
if (!Utils.isWindows())
return false;
let w: any = window;
let touchCapabilities = new w.Windows.Devices.Input.TouchCapabilities();
let ss = stringify(touchCapabilities);
Utils.logger.info(ss);
let keyboardCapabilities = new w.Windows.Devices.Input.KeyboardCapabilities();
ss = stringify(keyboardCapabilities);
Utils.logger.info(ss);
Utils.logger.info('isUsingWindowSoftKeyboard end')
return true;
} catch (error) {
Utils.logger.error(`Utils.isUsingWindowSoftKeyboard: ${error}`);
return false;
}
}
在上面当我注销 ss 变量(我似乎能够在 Windows 上获得任何调试信息的唯一方法)时,它只是空的,所以我假设调用 TouchCapabilities() 和 @987654324 @ 只是不工作。
是否有人对我如何能够做到我上面描述的事情有任何其他建议?
[EDIT1]
我做得还不够。尽管stringify 没有显示任何内容,但至少其中一个属性确实存在(也许它们实际上是getters)。
无论如何,以下 DID 在桌面上运行时为键盘返回 1..
let keyboardCapabilities = new w.Windows.Devices.Input.KeyboardCapabilities();
Utils.logger.info(`Keyboard present: ${keyboardCapabilities.keyboardPresent}`);
我还尝试了触摸功能属性...
let touchCapabilities = new w.Windows.Devices.Input.TouchCapabilities();
Utils.logger.info(`touch present: ${touchCapabilities.TouchPresent}`);
Utils.logger.info(`Contacts: ${touchCapabilities.Contacts}`);
这两个都报告了undefined,但它确实表明属性touchCapabilitiesIS已定义,否则我在尝试访问这两个属性时会遇到空异常。
我现在只需要在带有触摸屏的平板电脑上进行测试(我可以在回到办公室时使用 Surface 平板电脑进行测试)。
我需要重新调查的另一个途径是使用getForCurrentView(),如下所示..
let w: any = window;
let vm = w.Windows.UI.ViewManagement;
this.logger.info(`vm is ${(vm ? "defined" : "undefined")}`);
let ip = vm.InputPane;
this.logger.info(`ip is ${(ip ? "defined" : "undefined")}`);
let inputPane = vm.InputPane.getForCurrentView();
this.logger.info(`inputPane is ${(inputPane ? "defined" : "undefined")}`);
if (inputPane) {
inputPane.addEventListener('show', async ev => {
this.logger.info(`inputPane show fired !!!`);
})
}
我没有让上述方法起作用,但我确实看到在其他搜索中提到了getForCurrentView(),即使在winjs 的上下文中也是如此。也许我打错地方了,所以再试一次。如果这可行,那就更好了,因为使用 show 和 'hide' 事件我可以调整应用程序的大小,即使用户手动关闭软键盘而不是依赖输入 focus 和 blur。
【问题讨论】:
标签: cordova ionic-framework uwp