键盘重叠文本框

时间:2014-04-28 13:29:06

标签: c# xaml windows-phone-8 windows-phone

我在WP8应用程序中使用文本框列表来注册文档。

文本框的数量非常大,因此用户必须在它们之间滚动。 为了在一个字段到另一个字段之间导航,我添加了两个applicationbarIcons,next和previous。按下接下来将焦点从列表更改为下一个文本框,并使用文本框的高度滚动滚动查看器的内容(在本例中为50)。

但是,有时,当将焦点切换到下面的元素时,键盘会覆盖文本框。 (内容不向上滚动)。

有没有办法强制文本框移动到键盘上方,即使它在滚动视图中?

<ScrollViewer x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel>
            <TextBlock Text="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.STRING_CONTACT}" Margin="10,5" FontWeight="SemiBold" Foreground="#878780"></TextBlock>
            <StackPanel Margin="10,5" Height="190" Background="#F4F3F4">
                <TextBox LostFocus="firstNameTxt_LostFocus_1" GotFocus="firstNameTxt_GotFocus_1" Margin="0,-7" FontSize="23" x:Name="firstNameTxt"   BorderThickness="0" Background="Transparent" InputScope="PersonalFullName"><TextBox>
                <TextBox LostFocus="firstNameTxt_LostFocus_1" GotFocus="firstNameTxt_GotFocus_1" Margin="0,-7" FontSize="23" x:Name="lastNameTxt"    BorderThickness="0" Background="Transparent" InputScope="PersonalFullName"></my:DefaultTextBox>
                <TextBox LostFocus="firstNameTxt_LostFocus_1" GotFocus="firstNameTxt_GotFocus_1" Margin="0,-7" FontSize="23" x:Name="MobileTxt"  BorderThickness="0" InputScope="Number" Background="Transparent" ></TextBox>
                <TextBox LostFocus="firstNameTxt_LostFocus_1" GotFocus="firstNameTxt_GotFocus_1" Margin="0,-7" FontSize="23" x:Name="EmailTxt" BorderThickness="0" Background="Transparent">
        </StackPanel>
</ScrollViewer>

代码背后:

void left_Click(object sender, EventArgs e)
    {
        int index = this.controls.IndexOf(currentControl) - 1;
        if (index == -1)
        {
            this.Focus();
            return;
        }

        currentControl = this.controls[index];
        ContentPanel.ScrollToVerticalOffset(ContentPanel.VerticalOffset - 50);
        currentControl.Focus();


    }

1 个答案:

答案 0 :(得分:6)

这是WP8的常见问题。当文本框被聚焦时,它将翻译Application的{​​{1}}以使其进入视图。在某些情况下(当剪贴板打开时,或者在您的情况下),这不能很好地工作。解决方法是在RootVisual的{​​{1}}和RootVisual事件上手动将GotFocus转换为所需的垂直偏移量。

LostFocus

在您的情况下,您可以取消自动翻译,并使TextBox滚动到private void TranslateRootVisualY(int yNew) { var rootFrame = Application.Current.RootVisual as PhoneApplicationFrame; rootFrame.RenderTransform = new CompositeTransform() {TranslateY = yNew}; } 事件中所需的偏移量:

ScrollViewer

GotFocus可以通过发件人和其他函数计算,例如private void firstNameTxt_GotFocus_1(object sender, RoutedEventArgs e) { TranslateRootVisualY(0); Dispatcher.BeginInvoke(() =>{ double destOffset; //...calculate destination offset ContentPanel.ScrollToVerticalOffset(destOffset); }); }

相关问题