Xamarin.Forms-为什么切换到另一页时键盘没有隐藏?

时间:2018-10-30 23:52:03

标签: xamarin xamarin.forms

该页面没有输入字段(只有一个标签),但是切换到该页面时键盘不会消失。这是正常行为吗?如何使键盘无法在新页面上打开?谢谢

首页xaml

merge = accounts.merge(withdrawals, on='person', how='left')

               amount_received        date_received              withdraw_date            withdraw_amount
segment        1  2  3  4   5         1   2   3   4   5          1    2    3    5         1     2     3     5
person
1              1  2  3  4   5        10  20  30  40  50        1.0  NaN  NaN  NaN        10.0   NaN   NaN   NaN
1              1  2  3  4   5        10  20  30  40  50        2.0  NaN  NaN  NaN        20.0   NaN   NaN   NaN
1              1  2  3  4   5        10  20  30  40  50        NaN  NaN  NaN  3.0         NaN   NaN   NaN  30.0
2              6  7  8  9  10        11  21  31  41  51        NaN  4.0  NaN  NaN         NaN  40.0   NaN   NaN
2              6  7  8  9  10        11  21  31  41  51        NaN  NaN  5.0  NaN         NaN   NaN  50.0   NaN

首页CodeFieldTextChanged方法

               amount_received        date_received              withdraw_date               withdraw_amount
segment        1  2  3  4   5         1   2   3   4   5         1    2    3    4    5        1     2     3    4     5
person
1              1  2  3  4   5        10  20  30  40  50        1.0  NaN  NaN  NaN   NaN     10.0   NaN   NaN   NaN   NaN
1              1  2  3  4   5        10  20  30  40  50        2.0  NaN  NaN  NaN   NaN     20.0   NaN   NaN   NaN   NaN
1              1  2  3  4   5        10  20  30  40  50        NaN  NaN  NaN  NaN   3.0     NaN    NaN   NaN   NaN   30.0
2              6  7  8  9  10        11  21  31  41  51        NaN  4.0  NaN  NaN   NaN     NaN    40.0  NaN   NaN   NaN
2              6  7  8  9  10        11  21  31  41  51        NaN  NaN  5.0  NaN   NaN     NaN    NaN   50.0  NaN   NaN

主页xaml

<ContentPage.Content>
    <StackLayout x:Name="stackLayout2">
        <Label x:Name="code_label" HorizontalOptions="CenterAndExpand" Font="Bold, Micro" TextColor="black" FontSize="19" Margin="15, 30, 15, 0" />
        <Entry x:Name="code_field" TextChanged="CodeFieldTextChanged"  Keyboard="Telephone" MaxLength="4" Margin="15, 30, 15, 0" />
    </StackLayout>
</ContentPage.Content>

如果private void CodeFieldTextChanged(object sender, TextChangedEventArgs args) { if (args.NewTextValue == "3333") { Application.Current.MainPage = new HomePage(); } } 替换了<ContentPage.Content> <StackLayout> <Label Text="Authorization ok!" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" /> </StackLayout> </ContentPage.Content> ,则键盘消失了,但在第一种情况下,键盘停留了

1 个答案:

答案 0 :(得分:1)

此问题似乎仅在Android上发生。并且发生的事情是,您在更改页面时从未点击过“ Return”(电话键盘上的绿色复选标记),因此操作系统不会关闭键盘,因此您必须手动将其关闭。试试这个:

private void CodeFieldTextChanged(object sender, TextChangedEventArgs args)
{
    if (args.NewTextValue == "3333")
    {
        // Add the following two lines to dismiss the keyboard
        Entry entry = sender as Entry;
        entry.Unfocus();
        Application.Current.MainPage = new HomePage();
    }
}