Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Forms 1.4.2 Entry.Unfocus() leaves the keyboard showing

$
0
0

On Android, my Forms View has an entry control that opens the keyboard when focused, and dismisses it when completed. I have a toolbar item that unhides this entry and rehides it when pressed again however it only dismisses the completed keyboard and leaves a default one in place (at least that is what it looks like).

Some code:

MyViewModel -

public void OnSearch ()
{
    if (IsSearchVisible) {
        IsSearchFocused = false;
        IsSearchVisible = false;
        MessagingCenter.Send<MyViewModel> (this, "UnfocusSearch");
    } else {
        IsSearchFocused = true;
        IsSearchVisible = true;
        MessagingCenter.Send<MyViewModel> (this, "FocusSearch");
    }
    System.Diagnostics.Debug.WriteLine ("Search Visible: " + IsSearchVisible);
    System.Diagnostics.Debug.WriteLine ("Search Focused: " + IsSearchFocused);
}

public void OnSearchTextChanged (object sender, TextChangedEventArgs e)
{
    if (_backup != null) {
        if (e.OldTextValue != e.NewTextValue || string.IsNullOrEmpty (e.NewTextValue))
            Items = _backup.Where (a => a.Name.IndexOf (e.NewTextValue, StringComparison.OrdinalIgnoreCase) >= 0).ToList ();
    }
}

public void OnSearchCompleted (object sender, EventArgs e)
{
    IsSearchFocused = false;
    IsSearchVisible = false;
    MessagingCenter.Send<ListViewModel> (this, "UnfocusSearch");
}

MyView code-behind -

public ArrestsListPage ()
{
    InitializeComponent ();
    BindingContext = new ListViewModel ();
    ((MyViewModel)BindingContext).RestoreStateAsync (Application.Current.Properties);

    ToolbarItems.Insert (0, new ToolbarItem ("Toggle", "ic_action_view_as_grid.png", ((MyViewModel)BindingContext).SwitchToGrid, ToolbarItemOrder.Primary, 0));
    ToolbarItems.Insert (1, new ToolbarItem ("Search", "ic_action_search.png", ((MyViewModel)BindingContext).OnSearch, ToolbarItemOrder.Primary, 0));

    this.SetBinding (IsBusyProperty, "IsBusy");
    var search = this.FindByName<Entry> ("Search");
    search.TextChanged += ((MyViewModel)BindingContext).OnSearchTextChanged;
    search.Completed += ((MyViewModel)BindingContext).OnSearchCompleted;
}

protected override void OnAppearing ()
{
    MessagingCenter.Subscribe < MyViewModel> (this, "FocusSearch", (sender) => {
        var isSameInstance = ((MyViewModel)BindingContext).Equals (sender);
        if (isSameInstance) {
            this.FindByName<Entry>("Search").Focus();
        }
    });

    MessagingCenter.Subscribe < MyViewModel> (this, "UnfocusSearch", (sender) => {
        var isSameInstance = ((MyViewModel)BindingContext).Equals (sender);
        if (isSameInstance) {
            System.Diagnostics.Debug.WriteLine("Unfocus");
            this.FindByName<Entry>("Search").Unfocus();
        }
    });
}

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>