Windows 应用商店指南
🌐 Windows Store Guide
在 Windows 10 中,经典的 Win32 可执行文件迎来了新的兄弟:通用 Windows 平台。新的 .appx 格式不仅支持许多新的强大 API,例如 Cortana 或推送通知,而且通过 Windows 商店,还简化了安装和更新的进程。
🌐 With Windows 10, the good old win32 executable got a new sibling: The Universal
Windows Platform. The new .appx format does not only enable a number of new
powerful APIs like Cortana or Push Notifications, but through the Windows Store,
also simplifies installation and updating.
Microsoft 开发了一个将 Electron 应用编译为 .appx 包的工具,允许开发者使用新应用模型中的一些功能。 本指南解释了如何使用它——以及 Electron AppX 包的功能和限制。
🌐 Microsoft developed a tool that compiles Electron apps as .appx packages,
enabling developers to use some of the goodies found in the new application
model. This guide explains how to use it - and what the capabilities and
limitations of an Electron AppX package are.
背景和要求
🌐 Background and Requirements
Windows 10 “周年更新”能够通过与虚拟化的文件系统和注册表一起启动来运行 win32 .exe 二进制文件。这两者都是在编译时通过在 Windows 容器中运行应用和安装程序创建的,从而允许 Windows 精确识别安装进程中对操作系统所做的修改。将可执行文件与虚拟文件系统和虚拟注册表配对,使 Windows 能够实现一键安装和卸载。
🌐 Windows 10 "Anniversary Update" is able to run win32 .exe binaries by
launching them together with a virtualized filesystem and registry. Both are
created during compilation by running app and installer inside a Windows
Container, allowing Windows to identify exactly which modifications to the
operating system are done during installation. Pairing the executable with a
virtual filesystem and a virtual registry allows Windows to enable one-click
installation and uninstallation.
此外,exe 会在 appx 模型内启动——意味着它可以使用许多可用于通用 Windows 平台的 API。为了获得更多功能,Electron 应用可以与一个不可见的 UWP 后台任务配合使用,该任务会与 exe 一起启动——类似于作为一个助手运行后台任务、接收推送通知或与其他 UWP 应用通信。
🌐 In addition, the exe is launched inside the appx model - meaning that it can use
many of the APIs available to the Universal Windows Platform. To gain even more
capabilities, an Electron app can pair up with an invisible UWP background task
launched together with the exe - sort of launched as a sidekick to run tasks
in the background, receive push notifications, or to communicate with other UWP
applications.
要编译任何现有的 Electron 应用,请确保你具备以下条件:
🌐 To compile any existing Electron app, ensure that you have the following requirements:
- Windows 10 周年纪念更新(2016 年 8 月 2 日发布)
- Windows 10 SDK,可在此下载
- 至少节点 4(要检查,请运行
node -v)
然后,去安装 electron-windows-store 命令行工具(CLI):
🌐 Then, go and install the electron-windows-store CLI:
npm install -g electron-windows-store
步骤 1:打包你的 Electron 应用
🌐 Step 1: Package Your Electron Application
使用 @electron/packager(或类似工具)打包应用。确保删除最终应用中不需要的 node_modules,因为任何不实际需要的模块都会增加应用的大小。
🌐 Package the application using @electron/packager (or a similar tool).
Make sure to remove node_modules that you don't need in your final application, since
any module you don't actually need will increase your application's size.
输出应该大致如下所示:
🌐 The output should look roughly like this:
├── Ghost.exe
├── LICENSE
├── content_resources_200_percent.pak
├── content_shell.pak
├── d3dcompiler_47.dll
├── ffmpeg.dll
├── icudtl.dat
├── libEGL.dll
├── libGLESv2.dll
├── locales
│ ├── am.pak
│ ├── ar.pak
│ ├── [...]
├── node.dll
├── resources
│ └── app.asar
├── v8_context_snapshot.bin
├── squirrel.exe
└── ui_resources_200_percent.pak
步骤 2:运行 electron-windows-store
🌐 Step 2: Running electron-windows-store
在提升权限的 PowerShell(以“管理员身份运行”)中,使用所需参数运行 electron-windows-store,传入输入和输出目录、应用的名称和版本,并确认 node_modules 应该被扁平化。
🌐 From an elevated PowerShell (run it "as Administrator"), run
electron-windows-store with the required parameters, passing both the input
and output directories, the app's name and version, and confirmation that
node_modules should be flattened.
electron-windows-store `
--input-directory C:\myelectronapp `
--output-directory C:\output\myelectronapp `
--package-version 1.0.0.0 `
--package-name myelectronapp
一旦执行,该工具就开始工作:它将你的 Electron 应用作为输入,扁平化 node_modules。然后,它将你的应用归档为 app.zip。
使用安装程序和 Windows 容器,该工具创建一个“扩展”的 AppX 包——包括 Windows 应用清单 (AppXManifest.xml) 以及输出文件夹中的虚拟文件系统和虚拟注册表。
🌐 Once executed, the tool goes to work: It accepts your Electron app as an input,
flattening the node_modules. Then, it archives your application as app.zip.
Using an installer and a Windows Container, the tool creates an "expanded" AppX
package - including the Windows Application Manifest (AppXManifest.xml) as
well as the virtual file system and the virtual registry inside your output
folder.
一旦创建了扩展的 AppX 文件,该工具会使用 Windows 应用打包程序(MakeAppx.exe)从磁盘上的这些文件创建单文件 AppX 包。最后,该工具可以用来在你的计算机上创建一个受信任的证书,以签署新的 AppX 包。使用签名的 AppX 包后,CLI 还可以自动在你的计算机上安装该包。
🌐 Once the expanded AppX files are created, the tool uses the Windows App Packager
(MakeAppx.exe) to create a single-file AppX package from those files on disk.
Finally, the tool can be used to create a trusted certificate on your computer
to sign the new AppX package. With the signed AppX package, the CLI can also
automatically install the package on your machine.
步骤3:使用AppX包
🌐 Step 3: Using the AppX Package
为了运行你的软件包,你的用户需要安装 Windows 10 并具有所谓的“周年更新”——有关如何更新 Windows 的详细信息,请参见 这里。
🌐 In order to run your package, your users will need Windows 10 with the so-called "Anniversary Update" - details on how to update Windows can be found here.
与传统的 UWP 应用相比,打包应用目前需要经过手动验证进程,你可以申请 这里。与此同时,所有用户都可以通过双击安装你的包,因此如果你希望使用更简单的安装方法,提交到商店可能不是必要的。在受管理的环境中(通常是企业),Add-AppxPackage 可以使用 PowerShell Cmdlet 以自动化方式安装它。
🌐 In opposition to traditional UWP apps, packaged apps currently need to undergo a
manual verification process, for which you can apply here.
In the meantime, all users will be able to install your package by double-clicking it,
so a submission to the store might not be necessary if you're looking for an
easier installation method. In managed environments (usually enterprises), the
Add-AppxPackage PowerShell Cmdlet can be used to install it in an automated fashion.
另一个重要的限制是,编译后的 AppX 包仍然包含一个 win32 可执行文件,因此无法在 Xbox、HoloLens 或手机上运行。
🌐 Another important limitation is that the compiled AppX package still contains a win32 executable - and will therefore not run on Xbox, HoloLens, or Phones.
可选:使用后台任务添加 UWP 功能
🌐 Optional: Add UWP Features using a BackgroundTask
你可以将你的 Electron 应用与一个隐形的 UWP 后台任务配对,从而充分利用 Windows 10 的功能——比如推送通知、Cortana 集成或动态磁贴。
🌐 You can pair your Electron app up with an invisible UWP background task that gets to make full use of Windows 10 features - like push notifications, Cortana integration, or live tiles.
查看一个使用后台任务发送通知和动态磁贴的 Electron 应用,查看微软提供的示例。
🌐 To check out how an Electron app that uses a background task to send toast notifications and live tiles, check out the Microsoft-provided sample.
可选:使用容器虚拟化进行转换
🌐 Optional: Convert using Container Virtualization
要生成 AppX 包,electron-windows-store CLI 使用一个适用于大多数 Electron 应用的模板。然而,如果你使用自定义安装程序,或者在生成的包中遇到任何问题,你可以尝试使用 Windows 容器进行编译来创建包——在该模式下,CLI 会在空白的 Windows 容器中安装并运行你的应用,以确定你的应用对操作系统的具体修改。
🌐 To generate the AppX package, the electron-windows-store CLI uses a template
that should work for most Electron apps. However, if you are using a custom
installer, or should you experience any trouble with the generated package, you
can attempt to create a package using compilation with a Windows Container - in
that mode, the CLI will install and run your application in blank Windows Container
to determine what modifications your application is exactly doing to the operating
system.
在第一次运行 CLI 之前,你需要先设置“Windows 桌面应用转换器”。这将花费几分钟时间,但不用担心——你只需要做一次。从 这里 下载桌面应用转换器。
你将收到两个文件:DesktopAppConverter.zip 和 BaseImage-14316.wim。
🌐 Before running the CLI for the first time, you will have to setup the "Windows Desktop App
Converter". This will take a few minutes, but don't worry - you only have to do
this once. Download the Desktop App Converter from here.
You will receive two files: DesktopAppConverter.zip and BaseImage-14316.wim.
- 解压
DesktopAppConverter.zip。从提升权限的 PowerShell(以“以管理员身份运行”打开)中运行,确保你的系统执行策略允许我们通过调用Set-ExecutionPolicy bypass来运行我们打算执行的所有内容。 - 然后,运行桌面应用转换器的安装程序,传入 Windows 基础镜像的位置(已下载为
BaseImage-14316.wim),通过调用.\DesktopAppConverter.ps1 -Setup -BaseImage .\BaseImage-14316.wim。 - 如果运行上述命令时提示你重启,请重新启动你的计算机,并在成功重启后再次运行上述命令。
安装成功后,你可以继续编译你的 Electron 应用。
🌐 Once installation succeeded, you can move on to compiling your Electron app.