当前位置: 首页 > 文档资料 > Electron 中文文档 >

Windows 商店提交指南

优质
小牛编辑
183浏览
2023-12-01

在 Windows 10 中,一些不错的旧 win32 程序迎来了一个新朋友:通用 Windows 平台。 新的 .appx 格式不仅启用了许多新的强大的 API,如 Cortana 或推送通知,而且通过 Windows 应用商店,也同时简化了安装和更新。

Microsoft 开发了一个工具,将 Electron 应用程序编译为 .appx 软件包,使开发人员能够使用新应用程序模型中的一些好东西。 本指南解释了如何使用它 - 以及 Electron AppX 包的功能和限制。

Windows Store Guide

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 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.

背景和要求

Windows 10 的 "周年更新" 能够运行 win32 .exe 程序并且它们的虚拟化文件系统和注册表跟随一起启动。 两者都是通过在 Windows 容器中运行应用程序和安装器编译后创建的,允许 Windows 在安装过程中正确识别操作系统进行了哪些修改。 将可执行文件和虚拟文件系统与虚拟注册表配对, 允许 Windows 启用一键安装和卸载。

此外,exe 在 appx 模型内启动 - 这意味着它可以使用通用 Windows 平台可用的许多 API。 为了获得更多的功能,Electron 应用程序可以与一个看不见的 UWP 后台任务配合使用,它与 exe 一起启动,作为后台运行任务的接收器,接收推送通知或与其他 UWP 应用程序通信 。

要编译任何现有的 Electron 应用程序,请确保满足以下要求:

  • Windows 10及周年更新 (2016年8月2日发布的)
  • Windows 10 SDK, 这里下载
  • 最新的 Node 4 (运行 node -v 来确认)

然后, 安装 electron-windows-store CLI:

npm install -g electron-windows-store

Background and Requirements

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.

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.

To compile any existing Electron app, ensure that you have the following requirements:

  • Windows 10 with Anniversary Update (released August 2nd, 2016)
  • The Windows 10 SDK, downloadable here
  • At least Node 4 (to check, run node -v)

Then, go and install the electron-windows-store CLI:

npm install -g electron-windows-store

步骤 1: 打包你的 Electron 应用程序

打包应用程序使用 electron-packager (或类似工具). 确保在最终的应用程序中删除不需要的 node_modules, 因为这些你不需要模块只会额外增加你的应用程序的大小.

结构输出应该看起来大致像这样:

├── 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
│   ├── [...]
├── natives_blob.bin
├── node.dll
├── resources
│   ├── app
│   └── atom.asar
├── v8_context_snapshot.bin
├── squirrel.exe
└── ui_resources_200_percent.pak

Step 1: Package Your Electron Application

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
│   ├── [...]
├── natives_blob.bin
├── node.dll
├── resources
│   ├── app
│   └── atom.asar
├── v8_context_snapshot.bin
├── squirrel.exe
└── ui_resources_200_percent.pak

步骤 2: 运行 electron-windows-store

从提权的 PowerShell(用管理员身份运行它) 中, 以所需的参数运行 electron-windows-store,传递输入和输出目录,应用程序的名称和版本,以及确认 node_modules 应该是扁平的。

electron-windows-store `
    --input-directory C:myelectronapp `
    --output-directory C:outputmyelectronapp `
    --package-version 1.0.0.0 `
    --package-name myelectronapp

一旦执行,工具就开始工作:它接受您的 Electron 应用程序作为输入,展平 node_modules。 然后,它将应用程序归档为 app.zip。 使用安装程序和 Windows 容器,该工具创建一个“扩展的” AppX 包 - 包括 Windows 应用程序清单 (AppXManifest.xml)以及虚拟文件系统和输出文件夹中的虚拟注册表。

当创建扩展的 AppX 文件后,该工具使用 Windows App Packager(MakeAppx.exe)将磁盘上的这些文件创建为单文件 AppX 包。 最后,该工具可用于在计算机上创建可信证书,以签署新的 AppX 包。 使用签名的 AppX 软件包,CLI也可以自动在您的计算机上安装软件包。

Step 2: Running electron-windows-store

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:outputmyelectronapp `
    --package-version 1.0.0.0 `
    --package-name myelectronapp

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.

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 包

为了运行您的软件包,您的用户将需要将 Windows 10 安装“周年纪念更新” - 有关如何更新Windows的详细信息可以在这里找到

与传统的UWP应用程序不同,打包应用程序目前需要进行手动验证过程,您可以在这里申请. 在此期间,所有用户都能够通过双击安装包来安装您的程序,所以如果您只是寻找一个更简单的安装方法,可能不需要提交到商店。 在受管理的环境中(通常是企业), Add-AppxPackage PowerShell Cmdlet 可用于以自动方式安装它。

另一个重要的限制是编译的 AppX 包仍然包含一个 win32 可执行文件,因此不会在 Xbox,HoloLens 或 Phones 中运行。

Step 3: Using the AppX Package

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.

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.

Another important limitation is that the compiled AppX package still contains a win32 executable - and will therefore not run on Xbox, HoloLens, or Phones.

可选: 使用 BackgroundTask 添加 UWP 功能

您可以将 Electron 应用程序与不可见的 UWP 后台任务配对,以充分利用 Windows 10 功能,如推送通知,Cortana 集成或活动磁贴。

如何使用 Electron 应用程序通过后台任务发送 Toast 通知和活动磁贴, 请查看微软提供的案例.

Optional: Add UWP Features using a BackgroundTask

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.

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.

可选: 使用容器虚拟化进行转换

要生成 AppX 包,electron-windows-store CLI 使用的模板应该适用于大多数 Electron 应用程序。 但是,如果您使用自定义安装程序,或者您遇到生成的包的任何问题,您可以尝试使用 Windows 容器编译创建包 - 在该模式下,CLI 将在空 Windows 容器中安装和运行应用程序,以确定应用程序正在对操作系统进行哪些修改。

在第一次运行 CLI 之前,您必须安装 “Windows Desktop App Converter” 。 这将需要几分钟,但不要担心 - 你只需要这样做一次。 从这里下载 Desktop App Converter 您将得到两个文件: DesktopAppConverter.zipBaseImage-14316.wim.

  1. 解压 DesktopAppConverter.zip. 打开提权的 PowerShell (用"以管理员权限运行"打开, 确保您的系统执行策略允许我们通过调用 Set-ExecutionPolicy bypass 来运行我们想要运行的一切).
  2. 然后, 通过调用 .DesktopAppConverter.ps1 -Setup -BaseImage .BaseImage-14316.wim, 运行 Desktop App Converter 安装,并传递 Windows 基本映像的位置 (下载的 BaseImage-14316.wim).
  3. 如果运行以上命令提示您重新启动,请重新启动计算机,并在成功重新启动后再次运行上述命令。

当安装成功后,您可以继续编译你的 Electron 应用程序。

Optional: Convert using Container Virtualization

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.

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.

  1. 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 calling Set-ExecutionPolicy bypass.
  2. 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.
  3. If running the above command prompts you for a reboot, please restart your machine and run the above command again after a successful restart.

Once installation succeeded, you can move on to compiling your Electron app.