1

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.

1 Answers1

2

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);
john-g
  • 874
  • 6
  • 14
  • I've tried the method that has been mentioned in our index.html code but still when we launch the app from the tab we're not able to move the focus at all until we click on the controller home button and close the xbox menu. Is there any part of the code that I'm missing? – Tharun Koti Aug 23 '23 at 07:06
  • 1
    In your uwp/c# code you need to programmatically set focus onto the webview control after loading the webview source url. The extra click you are having to make is setting focus from the uwp app to inside the webview. but you can do this in your code-behind in the c# – john-g Aug 24 '23 at 14:56
  • Thanks John for your reply and the solution that has been provided by you works perfectly!!! – Tharun Koti Aug 25 '23 at 06:16