通常当TextBox
聚焦时,软键盘会打开,但问题是默认情况下,当键盘打开时页面向上移动,当用户通过点击键盘关闭键盘时再次移动前一个位置屏幕。
我需要将页面切换到某个Y.当我点击“完成”按钮时,我需要能够关闭键盘,而不仅仅是当我点击屏幕时。
我正在尝试查找正在滚动的页面的默认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位于正确的位置,仅适用于这两个分辨率。
问题是我应该在所有屏幕分辨率上以相同的方式工作。任何帮助我如何使我的代码动态适用于所有屏幕尺寸?
答案 0 :(得分:1)
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
有一个相当不错的例子here