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 应用可以与随 exe
一起启动的不可见 UWP 后台任务配对 - 作为一个助手启动,在后台运行任务、接收推送通知或与其他 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 with Anniversary Update (released August 2nd, 2016)
-
Windows 10 SDK,可以在这里下载
¥The Windows 10 SDK, downloadable here
-
至少节点 4(要检查,运行
node -v
)¥At least Node 4 (to check, run
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:运行 electro-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 容器创建 "expanded" 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 App Packager (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 应用如何使用后台任务发送 Toast 通知和动态磁贴,查看 Microsoft 提供的示例.
¥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 and Desktop App Converter from here.
You will receive two files: DesktopAppConverter.zip
and BaseImage-14316.wim
.
-
解压缩
DesktopAppConverter.zip
。从提升的 PowerShell(使用 "以管理员身份运行" 打开)中,确保你的系统执行策略允许我们通过调用Set-ExecutionPolicy bypass
。¥Unzip
DesktopAppConverter.zip
. From an elevated PowerShell (opened with "run as Administrator", ensure that your systems execution policy allows us to run everything we intend to run by callingSet-ExecutionPolicy bypass
. -
然后,运行 Desktop App Converter 的安装,通过调用
.\DesktopAppConverter.ps1 -Setup -BaseImage .\BaseImage-14316.wim
传入 Windows 基础映像(下载为BaseImage-14316.wim
)的位置。¥Then, run the installation of the Desktop App Converter, passing in the location of the Windows base Image (downloaded as
BaseImage-14316.wim
), by calling.\DesktopAppConverter.ps1 -Setup -BaseImage .\BaseImage-14316.wim
. -
如果运行上述命令提示重启,请重新启动机器,重启成功后再次运行上述命令。
¥If running the above command prompts you for a reboot, please restart your machine and run the above command again after a successful restart.
安装成功后,你可以继续编译你的 Electron 应用。
¥Once installation succeeded, you can move on to compiling your Electron app.