NavigationBar disable focus on back button in handheld device

cksanjcksanj USMember ✭✭

Hi All,

I am working with Xamarin.Forms NavigationPage and ContentPage for handheld scanner Motorola. I am trying to handle the 'Enter' hardware button for the button click on page. By default, the focus is on Navigation back button and when I press Enter button, I go to previous ContentPage. However, I want the default focus to be on Button [instead of NavigationBar back button] that I have in ContentPage. I can navigate with arrow keys and press the button on page. But I want the focus to be in button when the page first loads.

What I tried are:-
1. Remove the NavigationBar with NavigationPage.SetHasNavigationBar(). In this case, the NavigationBar is hidden, however still the focus on hidden back button.
2. Disable the back button and Icon with NavigationPage.SetHasBackButton() and NavigationPage.SetHasTitleIcon().

Any code or idea for handling the issue would be really helpful.

Thank you.

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @cksanj - I have a page class MyAppContentPageView that inherits from ContentPage, with my app's pages then using that page class.

    public class MyAppContentPageView : ContentPage
    

    Within MyAppContentPageView, I have a property that allows me to specify the View that I want to receive the initial focus:

        public View ViewToReceiveInitialFocus { get; set; }
    

    I override OnAppearing within MyAppContentPageView, and within my override identify whether this page is appearing because it has just been pushed onto the Navigation stack or whether it is re-appearing as a result of another page being popped from the Navigation stack. If it's the result of being pushed, I call a virtual method, OnFirstAppearance. OnFirstAppearnce sets the initial focus as follows:

        protected virtual void OnFirstAppearance()
        {
            try
            {
                ViewToReceiveInitialFocus?.Focus();
            }
            catch (Exception ex)
            {
                CommonInfrastructure.InsightsWrapper.ReportException(ex);
            }
        }
    

    Then, when I am creating a page derived from MyAppContentPageView, as part of the creation of the UI hierarchy (that eventually gets assigned to MyAppContentPageView.Content), if I want to explicitly control where the initial focus goes, I simply set ViewToReceiveInitialFocus to the appropriate View.

    IMHO, any app that may run on a device with a physical keyboard (or extra software to support accessibility) should handle stuff like this, both to support accessibility (which can be a legal requirement) and to support power users who find keyboard navigation quicker. Xamarin still have some work to do in order to support keyboard navigation out-of-the-box, but with the Microsoft acquisition of Xamarin I suspect (hope) that will get a higher priority now.

    Hope this helps.

  • cksanjcksanj USMember ✭✭

    @JohnHardman

    Hey John,

    Thank you for the response. However, I tried to override the OnAppearing() and OnFirstAppearance() method and focus my view; it didn't work. When the page initially loads then OnAppearing() method is called but OnFirstAppearance() is not being called.

    Any idea what's going on ?

    Thank you.

  • JohnHardmanJohnHardman GBUniversity mod

    @cksanj - Yes, OnFirstAppearance is not a built-in method. It's something I added. You need logic inside OnAppearing to call OnFirstAppearance as appropriate. I'm in the midst of a major re-factoring right now, but if you need help with that logic, ping me a message and I'll help out when I'm free.

  • cksanjcksanj USMember ✭✭

    @JohnHardman
    I solved the issue with a dirty trick. It's working for now. Maybe I would need a better way for it.

    For now, I made hidden Entry where I focused and handled it's Completed event for button event.

    Thank you for the help John.

Sign In or Register to comment.