2
votes

I know the solution on Android

@Override
 public boolean onKeyDown(int keyCode, KeyEvent event) 
 {
        switch(keyCode){
        case KeyEvent.KEYCODE_BACK:
            // do something here 
            return true;
 }
        return super.onKeyDown(keyCode, event); }

But i cant do it on the forms side. The focus is an Entry so the keyboard is visible and when i press hardware back button (keyboard disappear) i'd like to set my variable value.

Edited:

bool _tapped;

LwTeszt.PropertyChanged += (s, e) =>
            {
                if (LwTeszt.SelectedItem != null)
                {
                    EntryTeszt.Focus();
                }

            };

            EntryTeszt.Unfocused += EntryTeszt_Unfocus;

        private void EntryTeszt_Unfocus(object sender, FocusEventArgs e)
        {
            _tapped = true;
        }

        private void ViewCell_Tapped(object sender, EventArgs e)
        {

            if (_tapped)
            {
                EntryTeszt.Unfocus();
                _tapped = false;
                return;
            }

            EntryTeszt.Focus();

        }
2

2 Answers

1
votes

Unless you have additional logic that you didn't post I think you might be overcomplicating things. I believe all you need to do is focus your entry on ViewCell_Tapped

private void ViewCell_Tapped(object sender, EventArgs e)
{
    EntryTeszt.Focus();
}

Forms will handle unfocusing your Entry for you when back is pressed and in that case you won't need the LwTeszt.PropertyChanged, _tapped, and the EntryTeszt.Unfocused parts.

1
votes

There is an Event you have to just override that and you can handle back button in Xamarin forms. Use below code for handling back button:

    protected override bool OnBackButtonPressed()
    {
        // your code
        return base.OnBackButtonPressed();
    }

Also you can refer this question for more information.