InputPane 无法正常工作
InputPane does not work correctly
我目前正在开发一个通用应用程序,但是有一个问题。我有一个带有用户 Phone 编号文本框的框架。
所以,我想更改 LayoutRoot (GRID) 的高度,以便它可以适应自由空间 space。
为此,我使用 InputPane.GetForCurrentView().Showing
和 InputPane.GetForCurrentView().Hiding
。
这是我的代码。
public UserRegistrationAuthorization_PhoneNumber()
{
this.InitializeComponent();
LayoutRootInitialHeight = LayoutRoot.ActualHeight;
InputPane.GetForCurrentView().Showing += UserRegistrationAuthorization_PhoneNumber_Showing;
InputPane.GetForCurrentView().Hiding += UserRegistrationAuthorization_PhoneNumber_Hiding;
}
private void UserRegistrationAuthorization_PhoneNumber_Showing(InputPane sender, InputPaneVisibilityEventArgs args)
{
LayoutRoot.Height = LayoutRoot.ActualHeight - args.OccludedRect.Height;
LayoutRoot.VerticalAlignment = VerticalAlignment.Top;
args.EnsuredFocusedElementInView = true;
}
private void UserRegistrationAuthorization_PhoneNumber_Hiding(InputPane sender, InputPaneVisibilityEventArgs args)
{
// TODO: Get rid of that shit
LayoutRoot.Height = LayoutRootInitialHeight;
args.EnsuredFocusedElementInView = false;
}
当我在 TextBox 外部单击时,键盘会隐藏并在屏幕上留下一个黑洞。 2
但是,最有趣的是,当我按下 Lumia 上的物理后退按钮时,键盘会正常隐藏,而我的 LayoutRoot 会获得框架的初始高度。
这是一个错误还是我做错了什么?
发生这种情况是因为当您在构造函数中保存 LayoutRootInitialHeight 时,LayoutRoot 实际上并未加载,它的 ActualHeight == 0。然后您将 LayoutRoot.Height 设置为 0,因此它变得不可见。因此,您应该将 LayoutRootInitialHeight 保存在 LayoutRoot 的 Loaded 事件处理程序中。
我还建议您根本不要更改 LayoutRoot 的高度。它会导致您的整个可视树从头开始渲染,这通常是不好的做法。相反,修改所有必要元素的 RenderTransform,以便将它们移动到适当的位置。 RenderTransform 是处理屏幕上的移动和动画的正确方法,你可以通过像键盘一样向上移动 Next 按钮来实现一些不错的视觉效果。
您的代码大致如下所示:
<Button Content="Next" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center">
<Button.RenderTransform>
<CompositeTransform x:Name="NextButtonTransform" TranslateY="0"/>
</Button.RenderTransform>
</Button>
...
private void UserRegistrationAuthorization_PhoneNumber_Showing(InputPane sender, InputPaneVisibilityEventArgs args)
{
NextButtonTransform.TranslateY = -300;
EnsuredFocusedElementInView = true;
}
private void UserRegistrationAuthorization_PhoneNumber_Hiding(InputPane sender, InputPaneVisibilityEventArgs args)
{
NextButtonTransform.TranslateY = 0;
args.EnsuredFocusedElementInView = false;
}
更复杂的方法是 运行 一些情节提要,使您的“下一步”按钮以与键盘相同的速度上下移动,始终出现在它的顶部。虽然,由于 InputPane.GetForCurrentView().Showing 在键盘已经完全显示后被触发,您应该将所有动画连接到 TextBox.GotFocus 和 TextBox.LostFocus 事件。在手机上,当文本框有焦点时,键盘总是显示,所以它会很好地工作。
我目前正在开发一个通用应用程序,但是有一个问题。我有一个带有用户 Phone 编号文本框的框架。
所以,我想更改 LayoutRoot (GRID) 的高度,以便它可以适应自由空间 space。
为此,我使用 InputPane.GetForCurrentView().Showing
和 InputPane.GetForCurrentView().Hiding
。
这是我的代码。
public UserRegistrationAuthorization_PhoneNumber()
{
this.InitializeComponent();
LayoutRootInitialHeight = LayoutRoot.ActualHeight;
InputPane.GetForCurrentView().Showing += UserRegistrationAuthorization_PhoneNumber_Showing;
InputPane.GetForCurrentView().Hiding += UserRegistrationAuthorization_PhoneNumber_Hiding;
}
private void UserRegistrationAuthorization_PhoneNumber_Showing(InputPane sender, InputPaneVisibilityEventArgs args)
{
LayoutRoot.Height = LayoutRoot.ActualHeight - args.OccludedRect.Height;
LayoutRoot.VerticalAlignment = VerticalAlignment.Top;
args.EnsuredFocusedElementInView = true;
}
private void UserRegistrationAuthorization_PhoneNumber_Hiding(InputPane sender, InputPaneVisibilityEventArgs args)
{
// TODO: Get rid of that shit
LayoutRoot.Height = LayoutRootInitialHeight;
args.EnsuredFocusedElementInView = false;
}
当我在 TextBox 外部单击时,键盘会隐藏并在屏幕上留下一个黑洞。 2
但是,最有趣的是,当我按下 Lumia 上的物理后退按钮时,键盘会正常隐藏,而我的 LayoutRoot 会获得框架的初始高度。
这是一个错误还是我做错了什么?
发生这种情况是因为当您在构造函数中保存 LayoutRootInitialHeight 时,LayoutRoot 实际上并未加载,它的 ActualHeight == 0。然后您将 LayoutRoot.Height 设置为 0,因此它变得不可见。因此,您应该将 LayoutRootInitialHeight 保存在 LayoutRoot 的 Loaded 事件处理程序中。
我还建议您根本不要更改 LayoutRoot 的高度。它会导致您的整个可视树从头开始渲染,这通常是不好的做法。相反,修改所有必要元素的 RenderTransform,以便将它们移动到适当的位置。 RenderTransform 是处理屏幕上的移动和动画的正确方法,你可以通过像键盘一样向上移动 Next 按钮来实现一些不错的视觉效果。
您的代码大致如下所示:
<Button Content="Next" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center">
<Button.RenderTransform>
<CompositeTransform x:Name="NextButtonTransform" TranslateY="0"/>
</Button.RenderTransform>
</Button>
...
private void UserRegistrationAuthorization_PhoneNumber_Showing(InputPane sender, InputPaneVisibilityEventArgs args)
{
NextButtonTransform.TranslateY = -300;
EnsuredFocusedElementInView = true;
}
private void UserRegistrationAuthorization_PhoneNumber_Hiding(InputPane sender, InputPaneVisibilityEventArgs args)
{
NextButtonTransform.TranslateY = 0;
args.EnsuredFocusedElementInView = false;
}
更复杂的方法是 运行 一些情节提要,使您的“下一步”按钮以与键盘相同的速度上下移动,始终出现在它的顶部。虽然,由于 InputPane.GetForCurrentView().Showing 在键盘已经完全显示后被触发,您应该将所有动画连接到 TextBox.GotFocus 和 TextBox.LostFocus 事件。在手机上,当文本框有焦点时,键盘总是显示,所以它会很好地工作。