我有一个登录页面,在一个大图像徽标下有 3 个 Entry 控件,在页面底部有一个 Button 控件,当我单击顶部的 Entry 控件以使 Entry 成为焦点以开始键入时,键盘出现并覆盖了下面的输入控件和登录按钮不是最好的用户体验。
用户只能在 Xamarin.Forms.iOS 上手动向上滚动,但在 Android 上使用相同的代码 ScrollToAsync 我已经能够通过抓取父 ScrollView 并执行“ScrollView.ScrollToAsync”使焦点上的条目滚动到页面顶部(EntryControl, ScrollToPosition.Start, true);"
在 Android 上,页面底部的按钮也会通过在中间空白区域的 Grid Row 定义中使用 * 来向上移动。
似乎 Xamarin.Forms.iOS 在呈现时的行为与 Xamarin.Forms.Android 完全不同。我已经看到 ScrollToAsync 在页面加载时在 iOS 上存在一些问题,因为 ScrollView 在页面完全加载之前不存在。但是这个动作是在已经完全呈现的页面上进行的。我究竟做错了什么?
我已经尝试过这个 SO 中提到的延迟,但它没有帮助。ScrollToAsync 在 Xamarin.Forms 中不起作用
XAML
<Grid>
<ScrollView VerticalOptions="FillAndExpand"
BackgroundColor="White"
x:Name="ScrollViewContainer"
>
<Grid Margin="15, 0, 15, 10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" /><!--Logo-->
<RowDefinition Height="Auto" /><!--Entry1-->
<RowDefinition Height="Auto" /><!--Entry2-->
<RowDefinition Height="Auto" /><!--Entry3-->
<RowDefinition Height="*" />
<RowDefinition Height="Auto" /><!--Button-->
<RowDefinition Height="20" /><!--Empty padding-->
</Grid.RowDefinitions>
<Image Grid.Row="0"
Aspect="AspectFit"
Source="CompanyLogo"
HorizontalOptions="Start"
HeightRequest="300"
VerticalOptions="Start"
Margin="10, 20, 20, 0"
/>
<Entry Grid.Row="1"
x:Name="BranchName"
Placeholder="BranchName"
Focused="BranchName_Focused"
/>
<Entry Grid.Row="2"
x:Name="Username"
Placeholder="e.g. Batman "
Focused="Username_Focused"
/>
<Entry Grid.Row="3"
x:Name="Password"
Placeholder="Enter your password"
Focused="Password_Focused"
IsPassword="True"
/>
<Button Grid.Row="5"
x:Name="LoginButton"
Text="Log in"
/>
</Grid>
</ScrollView>
</Grid>
代码背后
private async void BranchName_Focused(object sender, FocusEventArgs e)
{
await ScrollViewContainer.ScrollToAsync(BranchName, ScrollToPosition.Start, true);
}