在Windows 8 應用程式中,當TextBox控制項獲得焦點時,輸入面板會彈出,如 果TextBox控制項處於頁面下半部分,則系統會將頁面上推是的TextBox不被輸入面 板蓋住,但是當TextBox是在FlipView控制項中時,系統不會將頁面上推,所以這種 情況下輸入框被輸入面板蓋住。具體原因不清楚,不知道是不是系統bug。
當輸入面板彈出,頁面上推的操作可以通過監聽InputPane的Showing和Hiding 事件來處理,既然當TextBox在FlipView控制項時,系統沒有很好的處理頁面上推, 那麼開發人員可以通過監聽InputPane的事件來自己處理上推操作。
Windows 8 的一個執行個體代碼Responding to the appearance of the on- screen keyboard sample中介紹了如果監聽處理InputPane的相關操作,參考此實 例以FlipView中的TextBox控制項為例並對執行個體代碼進行簡化處理。
執行個體中的InputPaneHelper是對InputPane的事件處理的封裝,直接拿來使用, InputPaneHelper代碼如下:
using System; using System.Collections.Generic; using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.Foundation; using Windows.UI.Xaml.Media.Animation; namespace HuiZhang212.Keyboard { public delegate void InputPaneShowingHandler(object sender, InputPaneVisibilityEventArgs e); public delegate void InputPaneHidingHandler(InputPane input, InputPaneVisibilityEventArgs e); public class InputPaneHelper { private Dictionary<UIElement, InputPaneShowingHandler> handlerMap; private UIElement lastFocusedElement = null; private InputPaneHidingHandler hidingHandlerDelegate = null; public InputPaneHelper() { handlerMap = new Dictionary<UIElement, InputPaneShowingHandler>(); } public void SubscribeToKeyboard(bool subscribe) { InputPane input = InputPane.GetForCurrentView(); if (subscribe) { input.Showing += ShowingHandler; input.Hiding += HidingHandler; } else { input.Showing -= ShowingHandler; input.Hiding -= HidingHandler; } } public void AddShowingHandler(UIElement element, InputPaneShowingHandler handler) { if (handlerMap.ContainsKey(element)) { throw new System.Exception("A handler is already registered!"); } else { handlerMap.Add(element, handler); element.GotFocus += GotFocusHandler; element.LostFocus += LostFocusHandler; } } private void GotFocusHandler(object sender, RoutedEventArgs e) { lastFocusedElement = (UIElement)sender; } private void LostFocusHandler(object sender, RoutedEventArgs e) { if (lastFocusedElement == (UIElement)sender) { lastFocusedElement = null; } } private void ShowingHandler(InputPane sender, InputPaneVisibilityEventArgs e) { if (lastFocusedElement != null && handlerMap.Count > 0) { handlerMap[lastFocusedElement](lastFocusedElement, e); } lastFocusedElement = null; } private void HidingHandler(InputPane sender, InputPaneVisibilityEventArgs e) { if (hidingHandlerDelegate != null) { hidingHandlerDelegate(sender, e); } lastFocusedElement = null; } public void SetHidingHandler(InputPaneHidingHandler handler) { this.hidingHandlerDelegate = handler; } public void RemoveShowingHandler(UIElement element) { handlerMap.Remove(element); element.GotFocus -= GotFocusHandler; element.LostFocus -= LostFocusHandler; } } }