【发布时间】:2021-11-01 06:45:30
【问题描述】:
我正在开发用于 Xbox 的托管应用程序的已安装部分的升级版本。我们将不再使用旧的 jsproj 类型,而是实施一个使用 XAML 的新项目。这是一个简单的结构,其中一个页面包含一个 Webview,它将显示应用程序托管部分的内容。
无论如何,当我在这个新应用程序中测试托管部分时,我注意到 WinRT 有一些奇怪的地方。我们有很多使用 WinRT 的 JavaScript,但其中一些没有按预期运行。我发现至少有两个函数因以下错误而失败。
错误 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 使用的运行时版本与 Xbox 上使用的不同,但降级目标版本也无济于事。
是什么导致了这些错误,有没有办法修复它们?
注意:我们可以不用调用来获取应用程序视图,但我们需要客户端处理来自SystemMediaTransportControls 的按钮按下的监听。这里的目标是首先启动应用程序的已安装部分,并在稍后的工作阶段更新托管部分的 JavaScript,但似乎该项目的旧版本和该项目的旧版本之间没有明确的 1:1关于 WinRt 如何工作的新内容。
【问题讨论】:
-
从哪里调用 SystemMediaTransportControls.getForCurrentView()?它需要在 UI 线程中完成。我在派生自 Page 的类的构造函数中进行调用。
-
这发生在应用程序的托管部分(在 JavaScript 中)。但据我所知,当从 webview 中的浏览器调用时,没有办法在 UI 线程上显式运行它?
-
您能否分享更多您在上述方法中调用的代码,它是否在 JavaScript 中调用?
-
@NicoZhu-MSFT 是的。它在 JavaScript 内部被调用。这里的部分神秘之处在于,它在 C# 中有效,但在 JavaScript 中无效,但仅在 Xbox One、Series X 或 Series S 上运行时才有效。当在 Windows 10 上运行时,这些方法在 C# 和 JavaScript 中有效。
-
顺便说一句,我更新了问题以显示实际代码。
标签: javascript c# uwp winrt-xaml