多个 WinRT 功能在 Xbox 上失败,但在 Windows 10 上没有

Several WinRT functions fail on Xbox, but not on Windows 10

我正在开发 Xbox 托管应用程序安装部分的升级版本。我们不再使用旧的 jsproj 类型,而是实施一个使用 XAML 的新项目。这是一个简单的结构,一个页面包含一个 Webview,它将显示应用程序托管部分的内容。

无论如何,当我在这个新应用程序中测试托管部分时,我注意到 WinRT 有一些奇怪的地方。我们有很多 JavaScript 使用 WinRT,但其中一些未按预期运行。我发现至少有两个函数因以下错误而失败。

错误 1 ​​(JavaScript):

  try {
    const applicationView = Windows.UI.ViewManagement.ApplicationView.getForCurrentView();
    applicationView.setDesiredBoundsMode(
      Windows.UI.ViewManagement.ApplicationViewBoundsMode.useCoreWindow,
    );
  } catch (e) {
    console.log(e); // Element not found
  }

错误 2 (JavaScript):

try {
  this.control = Windows.Media.SystemMediaTransportControls.getForCurrentView();
  this.control.isEnabled = true;
  this.control.isFastForwardEnabled = true;
  this.control.isNextEnabled = true;
  this.control.isPauseEnabled = true;
  this.control.isPlayEnabled = true;
  this.control.isPreviousEnabled = true;
  this.control.isRecordEnabled = true;
  this.control.isRewindEnabled = true;
  this.control.isStopEnabled = true;
  this.control.isChannelUpEnabled = true;
  this.control.isChannelDownEnabled = true;
  this.control.playbackStatus = MediaPlaybackStatus.playing;
  this.control.onbuttonpressed = this.onButtonPressed;
} catch (e) {
  console.log(e);
  /*
    Invalid window handle. Could not find an appropriate view to be associated 
    with this instance of the MediaPlaybackControl. Please make sure that a view
    has been initialized.
  */
}

这些相同的方法在 C# 中也有效:

using Windows.UI.ViewManagement;
using Windows.Media;
...
    public MainPage()
    {
        this.InitializeComponent();
        ApplicationView.GetForCurrentView()
            .SetDesiredBoundsMode(
                ApplicationViewBoundsMode.UseCoreWindow
            );
        this.control = SystemMediaTransportControls
            .GetForCurrentView();
    }
    // code is happy

这是我使用的目标和最低版本:

Target version: Windows 10, version 2004 (10.0; build 19041)
Minimum version: Windows 10, version 1809 (10.0; build 17763)

我还尝试将目标版本降级为:

Target version: Windows 10, version 1903 (10.0; build 18362)
Minimum version: Windows 10, version 1809 (10.0; build 17763)

非常奇怪的是,当我 运行 在 Windows 10 上使用具有相同托管内容的同一应用程序时,这些错误不会发生。但是,当我启动该应用程序时,这些错误确实发生了到 Xbox One 或 Xbox Series S|X 上。我猜 Windows 10 使用的 运行time 版本与 Xbox 上使用的版本不同,但降级目标版本也无济于事。

是什么导致了这些错误,有没有办法解决它们?

注意: 我们可以不用调用来获取应用程序视图,但我们需要客户端处理来自 SystemMediaTransportControls 的按钮按下事件。这里的目标是首先启动应用程序的已安装部分,并在稍后的工作阶段为托管部分更新 JavaScript,但似乎两者之间没有明确的 1:1该项目的旧版本和关于 WinRt 工作原理的新版本。

根据 Microsoft documentation,默认情况下,WebView 内容托管在桌面设备系列的 UI 线程上,并在所有其他设备(Xbox One)上关闭 UI 线程、X 系列或 S 系列……)。因此,当不需要默认值时,我们可以使用我们需要的 WebViewExecutionMode 值在 XAML 之外创建一个 WebView 实例。例如:

 var webView = new WebView(WebViewExecutionMode.SameThread);            
        webView.Source = new Uri("https://...");    
        WebViewParent.Child = webView; //assuming WebViewParent is present in XAML

根据我的测试,当我使用 WebViewExecutionMode.SeparateThread 创建 WebView 实例时确实发生了错误,但当我使用 WebViewExecutionMode.SameThread 或 WebViewExecutionMode.SeparateProcess.[=12= 时没有发生]