2017-06-22 40 views
2

我有一個UWP應用程序,當虛擬鍵盤打開時,我的機器人會被推上去。是否可以讓scrollviewer保持原位,並在虛擬鍵盤打開時讓文本框保持在視圖中。UWP虛擬鍵盤將內容向上推

我看到你可以訂閱開放和隱藏事件,但是這並沒有給我任何選擇ui元素可以保留或隱藏。 https://docs.microsoft.com/en-us/windows/uwp/input-and-devices/respond-to-the-presence-of-the-touch-keyboard

<Page 
    x:Class="VirtualKeyboardFix.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:VirtualKeyboardFix" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <ScrollViewer > 
      <Image Source="image.png" /> 
     </ScrollViewer> 
     <TextBox HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Margin="10" /> 
    </Grid> 
</Page> 

Image of robot with hidden face

+0

通常情況下,內容被拉高了,如果沒有足夠的空間供鍵盤顯現。這是這種情況嗎? –

+0

添加圖像是否有助於回答該問題?哦。我明白你的意思。在這種情況下,圖像大小與屏幕大小相同。 – Haydn

+0

您可以嘗試手動設置文本框的偏移量(負值)。 –

回答

3

是的,這就是答案賈斯汀XL。非常感謝。如果您想將它作爲回覆發佈而不是評論,那麼我會將其標記爲答案。

以防萬一任何人有同樣的問題。

namespace VirtualKeyboardFix 
{ 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class MainPage : Page 
    { 
     public MainPage() 
     { 
      this.InitializeComponent(); 

      InputPane.GetForCurrentView().Showing += MainPage_Showing; 
      InputPane.GetForCurrentView().Hiding += MainPage_Hiding; 
     } 

     private void MainPage_Showing(InputPane sender, InputPaneVisibilityEventArgs args) 
     { 
      args.EnsuredFocusedElementInView = false; 
      InputTextBox.Margin = new Thickness(10, 10, 10, args.OccludedRect.Height + 10); 
     } 

     private void MainPage_Hiding(InputPane sender, InputPaneVisibilityEventArgs args) 
     { 
      args.EnsuredFocusedElementInView = false; 
      InputTextBox.Margin = new Thickness(10, 10, 10, 10); 
     } 
    } 
} 

Fixed Image

+3

您應該將其標記爲答案。應得的。 ;) –