存在键盘时 Windows Phone 应用程序滚动问题

标签 windows xaml windows-phone-8 scroll scrollviewer

我有一个由几个 UI 元素组成的屏幕,主要是文本框。当 TextBox 获得焦点时,整个页面都会向上推,将页眉移到屏幕外。我希望标题位于固定位置,其余内容在 ScrollViewer 中向上推。理想情况下,我希望按预期放置偏移量并显示/隐藏键盘并正确聚焦到 UI 元素。正确滚动行为的一个很好的例子是 native 警报应用程序。 任何帮助表示赞赏。干杯!

as here as here as here

最佳答案

呈现 SIP 键盘时,PhoneApplicationFrame.TranslateTransform.Y 被设置为特定值。要更新布局,我们只需将上边距设置为指定值 (-s),之后 Silverlight 布局系统将修复该问题。

这里是 XAML 部分:

<Grid x:Name="LayoutRoot" >
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="WINDOWS PHONE" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Text="developer's ?" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>
    <Grid Grid.Row="1" Margin="12,0,12,0"></Grid>
    <TextBox Grid.Row="2" LostFocus="TextBox_OnLostFocus" />
</Grid>

C#部分

public partial class MainPage : PhoneApplicationPage
{
    private static double newValue = 0.0;
    public static readonly DependencyProperty TranslateYProperty = DependencyProperty.Register("TranslateY", typeof(double), typeof(MainPage), new PropertyMetadata(0d, OnRenderXPropertyChanged));

    public MainPage()
    {
        InitializeComponent();
        this.Loaded += BindToKeyboardFocus;
    }

    private void BindToKeyboardFocus(object sender, RoutedEventArgs e)
    {
        PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
        if (frame != null)
        {
            var group = frame.RenderTransform as TransformGroup;
            if (group != null)
            {
                var translate = group.Children[0] as TranslateTransform;
                var translateYBinding = new Binding("Y");
                translateYBinding.Source = translate;
                SetBinding(TranslateYProperty, translateYBinding);
            }
        }
    }

    public double TranslateY
    {
        get { return (double)GetValue(TranslateYProperty); }
        set { SetValue(TranslateYProperty, value); }
    }

    private static void OnRenderXPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if ((double)e.NewValue <= newValue)
            ((MainPage)d).UpdateTopMargin((double)e.NewValue);
        newValue = (double)e.NewValue;
    }

    private void UpdateTopMargin(double translateY)
    {
        LayoutRoot.Margin = new Thickness(0, -translateY, 0, 0);
    }

    private void TextBox_OnLostFocus(object sender, RoutedEventArgs e)
    { 
        LayoutRoot.Margin = new Thickness();
    }
}

我认为这会有所帮助。 这是我的 GitHub 链接- https://github.com/rezacse/fixedHeaderOnTextBoxFocus

关于存在键盘时 Windows Phone 应用程序滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18249771/

相关文章:

c# - Wpf DataGrid DoubleClick 有奇怪的行为

opencv - Canny Edge算法与OpenCv-Windows Phone 8

c# - 将 WP8 升级到 Silverlight WP8.1,有效负载包含两个或更多具有相同目标的文件

c# - 带有显示错误的 Internet 代理服务器的 Windows WCF 客户端服务器违反了协议(protocol)。部分=响应状态行

windows - 为什么在管道发生故障时重定向会起作用?

windows - 在 cygwin 下的 Windows 应用程序中有时无法识别输入 key

windows - 'ffmpeg' 不是内部或外部命令、可运行程序或批处理文件

c# - 图像已保存,但不会加载

c# - UWP CommandBar 底部有空隙

适用于 Windows Phone 8 的 C# IMAP SMTP 邮件客户端