概述
前面一篇 About Windows 10 SDK Preview Build 17110 中,我们简单介绍了 Multi-instance UWP Apps,今天结合开发过程详细讲解一下。
在 Windows 10 Version 1803 以前,UWP App 同一时间只能启动一个实例,而在 1803 开始,UWP App 可以通过开发者的配置选择来支持多实例。如果一个多实例 UWP App 正在运行,这时一个激活请求发送过来,平台不会直接激活当前的实例,而是会创建一个新的实例,运行在单独的进程中。
开发过程
配置多实例支持
多实例特性需要在 Visual Studio 中安装新的项目模板:Multi-Instance App Project Templates.VSIX, 安装后,使用 C# 和 C++ 都可以创建项目。
两个模板会被安装:
这两个模板都会在 manifest 文件中添加 SupportsMultipleInstances,其中 desktop4 和 iot2 前缀标志了项目只支持传统桌面 Windows 和 IoT 系统。manifest 配置如下,我们只保留了新增的部分:
<Package
...
xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4"
xmlns:iot2="http://schemas.microsoft.com/appx/manifest/iot/windows10/2"
IgnorableNamespaces="uap mp desktop4 iot2">
...
<Applications>
<Application Id="App"
...
desktop4:SupportsMultipleInstances="true"
iot2:SupportsMultipleInstances="true">
...
</Application>
</Applications>
...
</Package>
实际运行时,每次点击 App 的磁贴,都会启动一个新的实例。如下图中,App 显示了启动的时间,在任务栏和运行窗口可以看到,两个实例同一时间在运行状态。
多实例激活重定向
UWP App 对多实例的支持,可以让同一 App 的多个实例可以同时在运行。它运行开发者自己定义,是每次开启一个新的实例,还是重定向某个目前激活的应用。举例来说,让你想使用 App 编辑一个文件,而这个文件正在 App 中被编辑,这时就不应该再开启一个新的实例,而是应该重定向当前正在编辑文件的实例。这就会用到 Multi-Instance Redirection UWP app 模板。
Multi-Instance Redirection UWP app 模板和我们上面看到的一样,对 manifest 文件会做同样的调整。同时该模板会增加一个 Program.cs 文件,在文件中包含一个 Main() 方法,靠这个方法来实现多实例激活的重定向操作。
我们来重点看看 Program.cs 文件中的 Main() 方法
static void Main(string[] args)
{
// First, we'll get our activation event args, which are typically richer
// than the incoming command-line args. We can use these in our app-defined
// logic for generating the key for this instance.
IActivatedEventArgs activatedArgs = AppInstance.GetActivatedEventArgs();
// In some scenarios, the platform might indicate a recommended instance.
// If so, we can redirect this activation to that instance instead, if we wish.
if (AppInstance.RecommendedInstance != null)
{
AppInstance.RecommendedInstance.RedirectActivationTo();
}
else
{
// Define a key for this instance, based on some app-specific logic.
// If the key is always unique, then the app will never redirect.
// If the key is always non-unique, then the app will always redirect
// to the first instance. In practice, the app should produce a key
// that is sometimes unique and sometimes not, depending on its own needs.
uint number = CryptographicBuffer.GenerateRandomNumber();
string key = (number % 2 == 0) ? "even" : "odd";
var instance = AppInstance.FindOrRegisterInstanceForKey(key);
if (instance.IsCurrentInstance)
{
// If we successfully registered this instance, we can now just
// go ahead and do normal XAML initialization.
global::Windows.UI.Xaml.Application.Start((p) => new App());
}
else
{
// Some other instance has registered for this key, so we'll
// redirect this activation to that instance instead.
instance.RedirectActivationTo();
}
}
}
对于 key 的构造和判断,以及判断后的处理,是多实例重定向的关键,我们先看看 FindOrRegisterInstanceForKey(key) 和 IsCurrentInstance 的注释:
//
// 摘要:
// 如果另一个实例已注册该密钥,使用平台注册一个应用实例,或查找现有实例。
//
// 参数:
// key:
// 作为实例密钥的非空字符串。
//
// 返回结果:
// 表示已注册密钥的第一个应用的应用实例。
public static AppInstance FindOrRegisterInstanceForKey(string key);
//
// 摘要:
// 应用的当前实例是否是该实例定义的特定密钥的已注册实例。
//
// 返回结果:
// 指示当前应用是否为该应用的已注册实例的布尔值。
public bool IsCurrentInstance { get; }
后台任务和多实例
关于后台任务的多实例,官方有以下说明:
其他注意事项
关于多实例应用,官方文档还提示了一些额外的注意事项:
多实例应用的介绍就到这里,大家可以结合自己应用的实际场景,更加合理的设置 key 和判断条件来使用多实例,谢谢!