繁体   English   中英

打开键盘时防止页面屏幕向上滚动(C#-XAML-Windows Store App)

[英]Prevent page screen scrolling up when keyboard is opened (C#-XAML-Windows Store App)

通常,当TextBox聚焦时,软键盘将打开,但问题是默认情况下,当键盘打开时页面向上移动,当用户通过点击屏幕关闭键盘时再次移动前一个位置。

我需要将页面切换到某个Y.当我点击“完成”按钮时,我需要能够关闭键盘,而不仅仅是当我点击屏幕时。

我试图找到ScrollViewer的页面的默认ScrollViewer :TextBox上的两个事件:

private void PNotesTextBox_GotFocus(object sender, RoutedEventArgs e)
{
    var parentScrollViewer = FindParent<ScrollViewer>(this);
    parentScrollViewer.VerticalScrollMode = ScrollMode.Enabled;
    parentScrollViewer.ChangeView(null, offset, null, true);
    parentScrollViewer.UpdateLayout();
}

private void PNotesTextBox_Tapped(object sender, TappedRoutedEventArgs e)
{
        offset = e.GetPosition(PNotesTextBox).Y - 10;
}

在MainPage构造函数中:

    var inputPane = InputPane.GetForCurrentView();
    inputPane.Showing += OnShowingInputPane;


    private async void OnShowingInputPane(InputPane sender, InputPaneVisibilityEventArgs args)
       {
           await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
           {
              var parentScrollViewer = FindParent<ScrollViewer>(this);
              parentScrollViewer.VerticalScrollMode = ScrollMode.Enabled;
              offset = 580 - args.OccludedRect.Top;
              parentScrollViewer.ChangeView(null, offset, null, true);
              parentScrollViewer.UpdateLayout();
            });
       }

public static T FindParent<T>(FrameworkElement reference)
   where T : FrameworkElement
   {
       FrameworkElement parent = reference;
       while (parent != null)
       {
           parent = parent.Parent as FrameworkElement;

           var rc = parent as T;
           if (rc != null)
           {
              return rc;
           }
       }
       return null;
    }

当我在模拟器上使用10.6" 1024x768 (4:3 100%)10.6" 1366x768 (16:9 100%)部署我的应用程序时,它可以正常工作,并且键盘的显示方式是ScrollViewer与TextBox一起滚动正确的位置,只适用于这两个决议。

问题是我应该在所有屏幕分辨率上以相同的方式工作。 任何帮助我如何使我的代码动态适用于所有屏幕尺寸?

文档中

When the touch keyboard appears, it automatically repositions your UI to ensure that the focused element remains visible. This can cause other important areas of your UI to move off screen. However, you can disable the default behavior and make your own UI adjustments when the touch keyboard appears

Key presses on the touch keyboard raise KeyDown and KeyUp events just like key presses on hardware keyboards. However, the touch keyboard will not raise input events for Ctrl+A, Ctrl+Z, Ctrl+X, Ctrl+C, and Ctrl+V, which are reserved for text manipulation in the input control

有一个相当不错的例子在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM