Search code examples
c#xamluwpxbox

Focus navigation issue when I relaunch the application from the Games and Apps tab on XBOX


When I launch the app from my Visual Studio on the XBOX console using the "Remote Machine" method I'm able to do the navigation using the Gamepad and able to get the "KeyDown" events for the same. But when I close the app and launch it again from the "Games and Apps" tab and once the app is loaded totally and then if I try to do navigation using the Gamepad I'm not able to do the navigation and also the "keyDown" events are not getting triggered at all.

I need to click on the "XBOX button" on the controller and close the Side menu then I'll be able to navigate properly without any issue. But why I'm not able to do navigation once the app is launched.

I thought it might be due to some focus issue so I've added the respective code but still facing the same issue. It's kind of a blocked situation for the user since he'll not be able to exit the application even on clicking the B button.

mainpage.xaml.cs:

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            MyWebView.Focus(FocusState.Programmatic);
        }

for the webview I'm using this code as below.

mainpage.xaml:

<Grid>
    <WebView x:Name="MyWebView"
         Source="...">
    </WebView>
</Grid>

Solution

  • After making some research and going through all the Microsoft docs for the XBOX development I found one of the Stack Overflow question which mentioned the same issue but in a different scenario and used that method to get a fix for this in this SO link https://stackoverflow.com/a/55595531/15189804 and the comment that has been mentioned in this post https://stackoverflow.com/a/76707340/15189804

    There the same issue has been observed on the input box whereas in our scenario it is with the addEventListener.

    The main root cause which is causing this issue is that the focus is not being set to the WebView when the app is launched. Once I open the XBOX menu guide and close, it is setting the focus on to the WebView which is then able to listen to the events.

    To set the focus on to the WebView we need to add some part of the code explicitly in your code-behind which will help in setting the focus when the app is launched.

    private void MyWebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
        {
            MyWebView.Visibility = Visibility.Collapsed;
            MyWebView.Visibility = Visibility.Visible;
            MyWebView.Focus(FocusState.Keyboard);  //here I'm using the keyboard method which worked for me we can go with programmatic method as well.
        }
    

    Here in the code-behind file we're using the NavigationCompleted which will be called once the WebView is successfully loaded the required content and then we're making the Visibility to collapsed and visible again which will help in setting the focus on to the WebView and listen to the keyDown Events.

    Hope this helps and Happy Coding!!!