Windows 10 uwp hide soft keyboard even when I set focus to a control

匆匆过客 提交于 2019-12-13 00:17:06

问题


I have a Windows 10 mobile uwp app and I am having two issues.

First, I set focus to controls in the app. I do this by using the common call successfully

 Control.Focus(FocusState.Programmatic);

However, there are some cases where this does not work. Most times it does but for example, when my page loads, I am trying to set an initial focus in one of the fields and it does not work. I have tried this call in two places. First, in the constructor for the page, after InitializeComponenets and also in the override onNavigatedTo method. Where is the best place to call this and what are some reasons why it may not appear to work, particularly when a new page is instantiated?

Second, related to setting focus. I have a text box on my UI that I set control to with the same Programmatic focus call I listed above. However, the soft (on screen)keyboard shows when this happens. I dont want it to show up when I set focus Programmatically but then have it show if the user selects the field. The scenario is I have a barcode scanner. When the page loads, I set focus in code to the text box and it is therefore ready for me to set the text in the text box from code, based on the barcode scanner result. There is hardly ever a need for the user to type into this field. Therefore, I dont need or want to have the keyboard showing and taking up real estate. There is a rare case when I do allow them to still type the text in manually, for example, in the case the barcode does not read. They would then select the control (even though it may already have focus programmatically) which should set focus again but instead as cursor, touch or something and then I want to show the soft keyboard.

What is the best way to do this?

Thanks!


回答1:


I think the best place to call Focus() is in Loaded event handler of the same control which you trying to focus. When this control is fully loaded, it means it's ready for interaction, including focusing.

As for preventing on-screen keyboard to appear, the TextBox class has PreventKeyboardDisplayOnProgrammaticFocus property. Try to set it to true, this should solve your issue.




回答2:


as far as focusing anything else than the TextBox did not work for me anyway, I found a good solution:

I called:

using Windows.UI.ViewManagement;

InputPane.GetForCurrentView().TryHide();

and the Keyboard gets hidden.



来源:https://stackoverflow.com/questions/41754904/windows-10-uwp-hide-soft-keyboard-even-when-i-set-focus-to-a-control

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