webviewuwpuwp-xamlxbox

Navigation using Controller doesn't work after turning on the controller


Hi I'm trying to develop a UWP WebView App for XBOX using the web source URL. Currently, the app is working fine. But when we don't do any navigation using the Gamepad for a long time. The controller is turned off automatically. When we turn it on again we are not able to move the focus for some time and none of the key actions will work.

Is there any workaround that can be used to fix this issue. Since the focus will not work for sometime after the XBOX controller is turned on again.

We're using the Gamepad API to handle the focus navigation issue and VS2022 is being used to develop the UWP WebView App using C#.

Thanks in advance. Happy coding.


Solution

  • In order to use the Gamepad correctly. I.e. to scroll through some content in a Webview control, you need to inject some javascript code into the webview in combination with your html content. I recently faced this exact issue in a production Xbox I am responsible for.

    The issue was loading terms of use html into a webview and allowing user to scroll through it using the gamepad.

    The solution was to load the html content into a variable. Then inject javascript content (also stored in string var) and html into the webview. The execution of the javascript being what enables the gamepad.

    using (var wc = new HttpClient())
    {
        var html = await wc.GetStringAsync(new Uri(ContentUrl));
        var script = @"<script>
    navigator.gamepadInputEmulation = 'gamepad'; 
    window.external.notify(navigator.gamepadInputEmulation);
    var x = document.body;
    function ScrollUp() { document.body.scrollTop -= 100; }
    function ScrollDown() { document.body.scrollTop += 100; }
    </script></body>";
    
        ContentWebView.NavigateToString(html + script);                    
    }
    

    The key javascript code needed is:

    navigator.gamepadInputEmulation = 'gamepad'; 
    window.external.notify(navigator.gamepadInputEmulation);