2014-10-20 75 views
0

內部時,檢測虛擬鍵盤我有以下FlipView在WinRT的項目專注於一個TextBox UserControl的

<FlipView x:Name="Flip" GotFocus="FlipView_GotFocus" Grid.Row="1" ItemsSource="{Binding Controls, ElementName=pageRoot}" SelectedItem="{Binding SelectedControl, ElementName=pageRoot, Mode=TwoWay}"> 
     <FlipView.ItemTemplate> 
      <DataTemplate> 
       <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> 
        <ContentPresenter Content="{Binding}" /> 
       </Grid> 
      </DataTemplate> 
     </FlipView.ItemTemplate> 
    </FlipView> 

它裏面我會有幾個用戶控件有一些文本框,但是當我專注於一個虛擬鍵盤在其他文本框前面,當我有一個簡單的TextBoxs頁面時,虛擬鍵盤並不像它通常那樣「提升」應用程序。

有沒有辦法檢測鍵盤顯示並拉動應用程序的視圖?

下面是使用

<UserControl.Resources> 
    <ResourceDictionary> 
     <common:ByteArrayToBitmapImageConverter x:Key="ByteArrayToBitmapImageConverter" /> 
     <common:StringToValidityConverter x:Key="StringToValidityConverter" /> 
    </ResourceDictionary> 
</UserControl.Resources> 

<StackPanel> 
    <StackPanel Style="{StaticResource SubHeaderStyle}"> 
     <Image Source="/Images/Contract/Sales.png" Style="{StaticResource SubHeaderImageStyle}" /> 
     <TextBlock x:Uid="Sale" Style="{StaticResource SubHeaderTextStyle}" /> 
    </StackPanel> 

    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="50" /> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="Auto" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 

     <Image Grid.RowSpan="6" Width="300" Source="{Binding Picture, Converter={StaticResource ByteArrayToBitmapImageConverter}}" /> 

     <TextBlock x:Uid="SalesOffice" Grid.Row="1" Grid.Column="2" /> 
     <TextBox Grid.Row="1" Grid.Column="3" Text="{Binding Office, Mode=TwoWay}" Style="{StaticResource TextBoxStyle}" 
       common:TextBoxBehavior.Validity="{Binding Text, RelativeSource={RelativeSource Self}, Converter={StaticResource StringToValidityConverter}}" /> 

     <TextBlock x:Uid="SalesAgent" Grid.Row="2" Grid.Column="2" /> 
     <TextBox Grid.Row="2" Grid.Column="3" Text="{Binding AgentName, Mode=TwoWay}" Style="{StaticResource TextBoxStyle}" 
       common:TextBoxBehavior.Validity="{Binding Text, RelativeSource={RelativeSource Self}, Converter={StaticResource StringToValidityConverter}}" /> 

     <TextBlock x:Uid="MobilePhone" Grid.Row="3" Grid.Column="2" /> 
     <TextBox Grid.Row="3" Grid.Column="3" Text="{Binding MobilePhone, Mode=TwoWay}" Style="{StaticResource TextBoxStyle}" 
       common:TextBoxBehavior.Validity="{Binding Text, RelativeSource={RelativeSource Self}, Converter={StaticResource StringToValidityConverter}}" InputScope="Number" /> 

     <TextBlock x:Uid="EmailAddress" Grid.Row="4" Grid.Column="2" /> 
     <TextBox Grid.Row="4" Grid.Column="3" Text="{Binding EmailAddress, Mode=TwoWay}" Style="{StaticResource TextBoxStyle}" 
       common:TextBoxBehavior.Validity="{Binding Text, RelativeSource={RelativeSource Self}, Converter={StaticResource StringToValidityConverter}}" /> 
    </Grid> 
</StackPanel> 

的用戶控件IM之一,這裏是它的外觀 App when Keyboard is up

編輯: 看來,我可以

Windows.UI.ViewManagement.InputPane.GetForCurrentView().Showing 
Windows.UI.ViewManagement.InputPane.GetForCurrentView().Hidding 
檢測鍵盤

現在我只需要學習如何拉動我的觀點。

+0

此信息有幫助 https://social.msdn.microsoft.com/forums/windowsapps/en-us/7349d01d-dc0e-4e1c-9327-df90e00fbebf/how-to-handle-the-appearance-of-the -onscreen鍵盤 – Ric 2014-10-20 13:11:36

回答

0

如您所見,您可以使用InputPane的顯示和隱藏事件來檢測InputPane的可見性變化。 InputPaneVisibilityEventArgs包含它將覆蓋的OccludedRect,並且您可以將RenderTransform應用於頁面以將其翻轉。

<Page.RenderTransform> 
    <CompositeTransform x:Name="pageTransform"/> 
</Page.RenderTransform> 

而痛苦地天真的實現:

void Page_Showing(InputPane sender, InputPaneVisibilityEventArgs args) 
{ 
    // We probably want something more sophisticated to make sure the 
    // focus control and any dependencies are in view 
    pageTransform.TranslateY -= args.OccludedRect.Height; 
    // Tell the InputPane we already handled things so it doesn't move the page again 
    args.EnsuredFocusedElementInView = true; 
} 

void Page_Hiding(InputPane sender, InputPaneVisibilityEventArgs args) 
{ 
    pageTransform.TranslateY = 0; 
    args.EnsuredFocusedElementInView = true; 
} 

在現實中,你會想要做數學題,以確保必要的控制是在視圖中。您可以使用動畫使移位更平滑。如果你想獲得更多的創意,你可以切換到一個新的視覺狀態,只需要一個更友好的佈局編輯必要的領域。在你的UserControl案例中,你可能只想移動控件而不是頁面。

此外,還要在Windows 10桌面上進行測試,因爲軟鍵盤行爲與窗口應用程序的交互方式有點不同。