You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xbox从游戏和应用标签重启应用时的手柄导航焦点异常问题

问题描述

通过Visual Studio的「Remote Machine」方式在Xbox主机启动应用时,手柄可正常导航并触发KeyDown事件;但关闭应用后从「游戏和应用」标签重新启动,应用完全加载后手柄无法导航,KeyDown事件完全失效。必须按下手柄Xbox键打开侧边菜单再关闭,才能恢复正常导航。

尝试在OnNavigatedTo中给WebView设置焦点,但问题依旧,用户甚至无法按B键退出应用,陷入阻塞状态。

相关代码

mainpage.xaml.cs

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

mainpage.xaml

<Grid>
    <WebView x:Name="MyWebView"
         Source="...">
    </WebView>
</Grid>
可行的解决办法
  1. 等WebView加载完成后再设置焦点
    页面导航完成时WebView可能还未完全加载,提前设置焦点会失效。改为监听WebView的加载完成事件再设置:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    MyWebView.NavigationCompleted += MyWebView_NavigationCompleted;
}

private void MyWebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
    MyWebView.Focus(FocusState.Programmatic);
    MyWebView.NavigationCompleted -= MyWebView_NavigationCompleted;
}
  1. 强制激活应用窗口
    重新启动应用时,系统可能未将输入焦点分配给应用窗口,先激活窗口再设置焦点:
using Windows.UI.Core;

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    Window.Current.CoreWindow.Activate();
    MyWebView.Focus(FocusState.Programmatic);
}
  1. 直接监听全局按键事件
    如果WebView始终无法获取焦点,直接注册窗口的按键监听事件,确保B键能正常退出应用:
using Windows.UI.Core;

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
    Window.Current.CoreWindow.Activate();
}

private void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
{
    if (args.VirtualKey == Windows.System.VirtualKey.GamepadB)
    {
        Application.Current.Exit();
    }
    // 可在此添加其他手柄按键的处理逻辑
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    base.OnNavigatedFrom(e);
    Window.Current.CoreWindow.KeyDown -= CoreWindow_KeyDown;
}
  1. 让网页释放焦点
    若WebView加载的网页中存在自动抢占焦点的元素(如输入框),可通过脚本让网页失焦后再给WebView设置焦点:
private async void MyWebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
    await MyWebView.InvokeScriptAsync("eval", new[] { "document.activeElement.blur();" });
    MyWebView.Focus(FocusState.Programmatic);
}

内容的提问来源于stack exchange,提问作者Tharun Koti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 00:53:37