【问题标题】:Why isn't the TextBox inside ContentDialog automatically scroll above keyboard为什么 ContentDialog 中的 TextBox 不会自动在键盘上方滚动
【发布时间】:2015-07-04 19:29:34
【问题描述】:

我注意到如果TextBox 在Page 中,那么一切正常。每当TextBox 获得焦点时,它将滚动到键盘上方的正确位置,以便用户在键入时能够看到文本。无论出于何种原因,ContentDialog 的情况都有些不同。 TextBox 很容易被键盘覆盖。我是否缺少任何明显的设置?

我创建了一个默认的ContentDialog 并将代码复制到一个页面。并获得以下截图。除了左列的上层 XAML 元素是 <ContentDialog> 和右列的 <Page> 之外,其他一切都相同。

左图 - ContentDialog 键盘弹出前
右图 - 键盘弹出前的Page

左图 - 键盘弹出后ContentDialog
右图 - 键盘弹出后Page

下面是相关代码:

<StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
    <TextBox Name="email" Header="Email address"/>
    <PasswordBox Name="password" Header="Password"/>
    <CheckBox Name="showPassword" Content="Show password"/>

    <!-- Content body -->
    <TextBlock Name="body" Style="{StaticResource MessageDialogContentStyle}" TextWrapping="Wrap">
        <TextBlock.Text>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit,
                sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        </TextBlock.Text>
    </TextBlock>
</StackPanel>

为什么ContentDialog 内的TextBox 不像Page 那样滚动到键盘上方?

【问题讨论】:

    标签: windows windows-phone-8.1


    【解决方案1】:

    一旦我遇到了与 TextBox 类似的问题并找到了 answer here。基本上,一旦显示键盘,焦点元素就不会向上移动,您可以通过进行额外的转换来纠正这种行为:

    // add this code somewhere to your constructor of your page or content dialog - where the problematic TextBox is located
    Windows.UI.ViewManagement.InputPane.GetForCurrentView().Showing += (s, args) =>
    {
        const double extraHeightBuffer = 20.0;
    
        UIElement focused = FocusManager.GetFocusedElement() as UIElement;
        if (null != focused)
        {
            GeneralTransform gt = focused.TransformToVisual(this);
            Point focusedPoint = gt.TransformPoint(new Point(0, focused.RenderSize.Height - 1));
            double bottomOfFocused = focusedPoint.Y + extraHeightBuffer;
            if (bottomOfFocused > args.OccludedRect.Top)
            {
                var trans = new TranslateTransform();
                trans.Y = -(bottomOfFocused - args.OccludedRect.Top);
                this.RenderTransform = trans;
            }
            args.EnsuredFocusedElementInView = true;
        }
    };
    
    Windows.UI.ViewManagement.InputPane.GetForCurrentView().Hiding += (s, args) =>
    {
        var trans = new TranslateTransform();
        trans.Y = 0;
        this.RenderTransform = trans;
        args.EnsuredFocusedElementInView = false;
    };
    

    【讨论】:

    • 太棒了!像魅力一样工作。谢谢!
    猜你喜欢
    • 2018-08-21
    • 2023-02-04
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多