2012-08-27 61 views
15

我正在C#中爲Windows 8開發我的應用程序,而一個非常煩人的事情是即使所有文本框都失去焦點,觸摸鍵盤有時仍會保留在屏幕上。Windows 8 - 如何關閉觸摸鍵盤?

我讀了文章keyboard dismissal logic white paper,它解釋了從控制切換到控制時,即使控件可能不接受鍵盤輸入,鍵盤仍可以保持打開狀態。這將是我的情況,因爲我的所有內容都是在GridView或ListView中託管的。當用戶點擊屏幕上的任何項目時,水龍頭將着陸在這些控件上。這非常煩人,因爲鍵盤佔用了一半的屏幕,並且無法關閉鍵盤。

我試圖設置文本框被禁用,它並沒有影響。刪除鍵盤的唯一方法是將焦點設置在一個非常黑客的按鈕上。

我以爲我需要對「AutomationPeer」做些什麼,但我不清楚究竟該做什麼。有沒有辦法來覆蓋這種行爲?

編輯: 我想通了。目標是更改爲未在whitepaper中列出的GridView和GridView項目的控件類型。下面是我做,讓我消除鍵盤網格的代碼:

public class KeyboardUnfocusableGridView : GridView 
{ 
    private class KeyboardUnfocusableGridViewAutomationPeer : GridViewAutomationPeer 
    { 
     public KeyboardUnfocusableGridViewAutomationPeer(GridView owner) 
      : base(owner) 
     { 
     } 

     protected override AutomationControlType GetAutomationControlTypeCore() 
     { 
      return AutomationControlType.Custom; 
     } 

    } 

    private class KeyboardUnfocusableGridViewItemAutomationPeer : GridViewItemAutomationPeer 
    { 
     public KeyboardUnfocusableGridViewItemAutomationPeer(GridViewItem owner) 
      : base(owner) 
     { } 

     protected override AutomationControlType GetAutomationControlTypeCore() 
     { 
      return AutomationControlType.Custom; 
     } 

    } 

    private class KeyboardUnfocusableGridViewItem : GridViewItem 
    { 
     protected override AutomationPeer OnCreateAutomationPeer() 
     { 
      var baseItem = base.OnCreateAutomationPeer(); 
      return new KeyboardUnfocusableGridViewItemAutomationPeer(this); 
     } 


    } 

    protected override AutomationPeer OnCreateAutomationPeer() 
    { 
     var baseItem = base.OnCreateAutomationPeer(); 
     return new KeyboardUnfocusableGridViewAutomationPeer(this); 
    } 

    protected override Windows.UI.Xaml.DependencyObject GetContainerForItemOverride() 
    { 
     return new KeyboardUnfocusableGridViewItem(); 
    } 
} 

這是不幸的,我需要寫這麼多的代碼做一個簡單的事情。這絕對不是最佳的,因爲我需要爲我需要使用的每個ItemsControl執行此操作。

+0

如果所有的內容是在GridView或ListView,所有你需要做的是設置IsHitTestVisible屬性爲false,並且您的文本框將無法接受焦點,因此鍵盤不會彈出。不幸的是,你也會失去滾動。只是一個FYI ... –

+0

偉大的解決方案,我對這個問題無能爲力:)。同樣的技術也適用於ListView。 –

+0

您可以將您的解決方案移至下面的答案,以便我們可以從未答覆的列表中獲得此答案嗎?謝謝。 –

回答

3

您需要做的是將焦點設置爲不接受文本輸入的任何控件。但是,請注意,如果用戶手動顯示鍵盤(而不是由於TextBox接收焦點而自動顯示),則鍵盤將保持打開狀態。

退房這一下屏幕上的鍵盤更多信息真的好螺紋:

http://social.msdn.microsoft.com/Forums/pl/winappswithcsharp/thread/3c227262-1d2c-4382-9c50-5b71d2b5d823

+0

這不起作用,因爲我所有的控件都在GridView或ListView中。默認情況下,在GridView或ListView中的控件上設置焦點不會關閉鍵盤。我想我已經想出了一個辦法,我很快就在這裏發佈。 – thisnick

+8

很快定義? – Spiked3