英文:
Focus navigation issue when I relaunch the application from the Games and Apps tab on XBOX
问题
从我的 Visual Studio 在 Xbox 主机上使用 "Remote Machine" 方法启动应用程序时,我能够使用游戏手柄进行导航,并能够获取相应的 "KeyDown" 事件。但是,当我关闭应用程序,然后再从 "Games and Apps" 标签中重新启动应用程序,一旦应用程序完全加载,然后尝试使用游戏手柄进行导航时,我无法进行导航,而且 "keyDown" 事件根本不会触发。
我需要点击控制器上的 "XBOX 按钮" 并关闭侧边菜单,然后我才能正常进行导航,没有任何问题。但是为什么我在应用程序启动后无法进行导航呢?
我以为这可能是焦点问题,所以我已经添加了相应的代码,但仍然面临相同的问题。对于用户来说,这是一种受阻的情况,因为即使单击 B 按钮,他也无法退出应用程序。
mainpage.xaml.cs:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
MyWebView.Focus(FocusState.Programmatic);
}
对于 WebView,我使用以下代码。
mainpage.xaml:
<Grid>
<WebView x:Name="MyWebView"
Source="...">
</WebView>
</Grid>
英文:
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>
答案1
得分: 0
经过一些研究并查看了所有关于XBOX开发的Microsoft文档后,我发现了一个Stack Overflow问题,提到了相同的问题,但在不同的情况下,并使用了该方法来修复此问题,链接在这里 https://stackoverflow.com/a/55595531/15189804 ,以及在此帖子 https://stackoverflow.com/a/76707340/15189804 中提到的评论。
在那里,相同的问题已经在输入框上观察到,而在我们的情况下,是在addEventListener上。
导致这个问题的主要根本原因是在应用程序启动时没有将焦点设置到WebView。一旦我打开XBOX菜单指南并关闭,它就会将焦点设置到WebView上,然后才能监听事件。
为了将焦点设置到WebView上,我们需要在代码中明确添加一些部分,这将有助于在应用程序启动时设置焦点。
private void MyWebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
MyWebView.Visibility = Visibility.Collapsed;
MyWebView.Visibility = Visibility.Visible;
MyWebView.Focus(FocusState.Keyboard); //在这里,我使用了对我有效的键盘方法,我们也可以使用编程方法。
}
在代码文件中,我们使用了NavigationCompleted
,它将在WebView成功加载所需内容后调用,然后我们将可见性设置为折叠,然后再次设置为可见,这将有助于将焦点设置到WebView上并监听keyDown
事件。
希望这有所帮助,祝编码愉快!
英文:
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!!!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论