Xamarin表格:键盘无法隐藏

时间:2016-08-09 14:21:57

标签: xamarin xamarin.android xamarin.forms android-softkeyboard soft-keyboard

创建页面时键盘会隐藏,但是当我将焦点更改为另一个条目时,我会调用相同的功能,但软键盘并不隐藏?

ConfigService.cs

    public void HideKeyboard()
    {
        var inputMethodManager = Xamarin.Forms.Forms.Context.GetSystemService(Context.InputMethodService) as InputMethodManager;
        if (inputMethodManager != null && Xamarin.Forms.Forms.Context is Activity)
        {
            var activity = Xamarin.Forms.Forms.Context as Activity;
            var focusedView = activity.CurrentFocus;
            var token = focusedView == null ? null : focusedView.WindowToken;
            inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);
        }
    }

VerplaatsingPage.xaml.cs

namespace SI_Foodware.View
{
   public partial class VerplaatsingPage : ContentPage
   {
    VerplaatsingPageViewModel vm;

    public VerplaatsingPage (SubMenu subMenu)
    {
        InitializeComponent ();

        vm = new VerplaatsingPageViewModel(this, subMenu);
        BindingContext = vm;
    }

    public void OnClickDestination(object o, EventArgs e)
    {

    }

    public void OnClickSelect(object o, EventArgs e)
    {

    }

    public void OnClickCancel(object o, EventArgs e)
    {

    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        ent_drager.Focus();
        IConfigService configService = DependencyService.Get<IConfigService>();
        configService.HideKeyboard();    
    }
}
}

VerplaatsingPageViewModel.cs

    public void Keyboard()
    {
        IConfigService configService = DependencyService.Get<IConfigService>();
        configService.HideKeyboard();
    }


    public void OnScan(string scanProperty)
    {
        var list = db.GetAllItems<ContainerLine>();
        Artikel = list[0].ItemNo;
        Description = list[0].Description;
        LotNo = list[0].LotNo;
        NumberOfContainer = list[0].Quantity;
        KiloDrager = list[0].KgQuantity;
        UnitOfMeasure = list[0].UnitofMeasureCode;
        LocationFrom = list[0].BinCode;

        cp.FindByName<Entry>("ent_destination").Focus();
        Keyboard(); <-- Doesn't Work 
    }

1 个答案:

答案 0 :(得分:0)

我使用

解决了我的问题

inputMethodManager.ToggleSoftInputFromWindow(token,ShowSoftInputFlags.None,HideSoftInputFlags.None);

而不是

inputMethodManager.HideSoftInputFromWindow(token,HideSoftInputFlags.None);