Dac*_*dar 7 c# xaml windows-phone windows-phone-8
我正在使用一个文本框列表来在WP8应用程序中注册文档.
文本框的数量非常大,因此用户必须在它们之间滚动.为了在一个字段到另一个字段之间导航,我添加了两个applicationbarIcons,next和previous.按下接下来将焦点从列表更改为下一个文本框,并使用文本框的高度滚动滚动查看器的内容(在本例中为50).
但是,有时,当将焦点切换到下面的元素时,键盘会覆盖文本框.(内容不向上滚动).
有没有办法强制文本框移动到键盘上方,即使它在滚动视图中?
<ScrollViewer x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<TextBlock Text="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.STRING_CONTACT}" Margin="10,5" FontWeight="SemiBold" Foreground="#878780"></TextBlock>
<StackPanel Margin="10,5" Height="190" Background="#F4F3F4">
<TextBox LostFocus="firstNameTxt_LostFocus_1" GotFocus="firstNameTxt_GotFocus_1" Margin="0,-7" FontSize="23" x:Name="firstNameTxt" BorderThickness="0" Background="Transparent" InputScope="PersonalFullName"><TextBox>
<TextBox LostFocus="firstNameTxt_LostFocus_1" GotFocus="firstNameTxt_GotFocus_1" Margin="0,-7" FontSize="23" x:Name="lastNameTxt" BorderThickness="0" Background="Transparent" InputScope="PersonalFullName"></my:DefaultTextBox>
<TextBox LostFocus="firstNameTxt_LostFocus_1" GotFocus="firstNameTxt_GotFocus_1" Margin="0,-7" FontSize="23" x:Name="MobileTxt" BorderThickness="0" InputScope="Number" Background="Transparent" ></TextBox>
<TextBox LostFocus="firstNameTxt_LostFocus_1" GotFocus="firstNameTxt_GotFocus_1" Margin="0,-7" FontSize="23" x:Name="EmailTxt" BorderThickness="0" Background="Transparent">
</StackPanel>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)
代码背后:
void left_Click(object sender, EventArgs e)
{
int index = this.controls.IndexOf(currentControl) - 1;
if (index == -1)
{
this.Focus();
return;
}
currentControl = this.controls[index];
ContentPanel.ScrollToVerticalOffset(ContentPanel.VerticalOffset - 50);
currentControl.Focus();
}
Run Code Online (Sandbox Code Playgroud)
小智 6
这是WP8上的常见问题.当一个文本框的重点是,它会转化Application的RootVisual,使其视图.在某些情况下(当剪贴板打开时,或者在您的情况下),这不能很好地工作.解决方法是手动转换RootVisual为所需的垂直偏移GotFocus和LostFocus事件TextBox.
private void TranslateRootVisualY(int yNew)
{
var rootFrame = Application.Current.RootVisual as PhoneApplicationFrame;
rootFrame.RenderTransform = new CompositeTransform() {TranslateY = yNew};
}
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您可以消除自动翻译并ScrollViewer在GotFocus事件中滚动到所需的偏移量:
private void firstNameTxt_GotFocus_1(object sender, RoutedEventArgs e)
{
TranslateRootVisualY(0);
Dispatcher.BeginInvoke(() =>{
double destOffset;
//...calculate destination offset
ContentPanel.ScrollToVerticalOffset(destOffset);
});
}
Run Code Online (Sandbox Code Playgroud)
destOffset 可以从发件人和其他功能计算 GetRectFromCharacterIndex
| 归档时间: |
|
| 查看次数: |
2241 次 |
| 最近记录: |