Windows Phone获取键盘高度

时间:2013-06-27 00:07:02

标签: c# windows-phone-8

我正在开发一个Windows Phone 8应用程序,我需要在显示键盘时进行一些自定义处理。有什么方法可以让我在当前的方向上获得键盘的高度。

由于

4 个答案:

答案 0 :(得分:2)

我在Windows Phone 8.1中使用以下内容对其进行了管理,但文本框没有足够的空间,下面有一个保存按钮和上面的数据透视标题。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <!--ScrollViewer Background="#302010" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"-->
        <TextBox  x:Name="text" 
                  ManipulationDelta="textList_ManipulationDelta" ManipulationMode="Scale" 
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                  TextWrapping="Wrap" AcceptsReturn="True"
                  >
        </TextBox>
    <Rectangle x:Name="rectRed" Fill="Red" Grid.Row="2" Height="10" />
    <Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Fill="#30000000" />
    <StackPanel x:Name="stackAppBar" Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
        <AppBarButton Icon="Save" Label="Save" IsCompact="true" x:Name="buttonSave" Click="buttonSave_Click"/>
        <!--AppBarButton Icon="Delete" Label="Delete"  IsCompact="true"/-->
    </StackPanel>
    <!--Rectangle x:Name="rectKeyboard" Fill="Red" Grid.Row="2" Height="200" /-->
</Grid>

async void UCSetup_Loaded(object sender, RoutedEventArgs e)
    {
        // put text in box
        string s = await IO.ReadSetupFile();
        text.Text = s;

        InputPane.GetForCurrentView().Showing += UCSetup_Showing;
        InputPane.GetForCurrentView().Hiding += UCSetup_Hiding;
    }

    void UCSetup_Hiding(InputPane sender, InputPaneVisibilityEventArgs args)
    {
        var size = args.OccludedRect;
        rectRed.Height = size.Height;

    }

    void UCSetup_Showing(InputPane sender, InputPaneVisibilityEventArgs args)
    {
        var size = args.OccludedRect;
        rectRed.Height = size.Height;

    }

答案 1 :(得分:1)

在XAML框架中执行此操作没有好办法,但只要触发TextBox.GotFocus事件,您就可以触发代码。最简单的方法就是子类TextBox,并在触发父GotFocus事件时触发自己的自定义事件。

如果您正在编写一个纯本机应用程序,那么您可以通过检查窗口的遮挡区域来找到它。可在以下结尾找到此示例:http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj247546(v=vs.105).aspx

答案 2 :(得分:1)

获取键盘的实际高度并不是很好的方法,但是如果键盘导致屏幕滚动,因为文本框位于下半部分,您可以通过查看滚动显示它的滚动距离在根框架的偏移处。

有关详细信息/代码,请参阅How to determine the keyboard offset

答案 3 :(得分:0)

对于Windows Phone 8.1 RT,只需在xaml.cs文件中添加以下代码即可。

// This part in the constructor of the page
InputPane inputPane = InputPane.GetForCurrentView();
inputPane.Showing += InputPane_Showing;
inputPane.Hiding += InputPane_Hiding;

private void InputPane_Hiding(InputPane sender, InputPaneVisibilityEventArgs args) {
     // Do what you want.
}

private void InputPane_Showing(InputPane sender, InputPaneVisibilityEventArgs args) {
    Debug.WriteLine("Keyboard Height: "sender.OccludedRect.Height);
}
相关问题