Windows 8 鍵盤上推自訂處理

來源:互聯網
上載者:User

在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;           }       }   }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.