除了使用 URI 在远程设备上启动应用外,还可以在远程设备上运行和通信 应用服务 。 任何基于 Windows 的设备都可以用作客户端或主机设备。 这为你提供了几乎无限数量的方法来与连接的设备交互,而无需将应用引入前台。
在主机设备上设置应用服务
若要在远程设备上运行应用服务,必须已在该设备上安装了该应用服务的提供程序。 本指南将使用 随机数生成器应用服务示例的 CSharp 版本,该示例在 Windows 通用示例存储库中提供。 有关如何编写自己的应用服务的说明,请参阅 创建和使用应用服务。
无论是使用已制作的应用服务还是编写自己的应用服务,都需要进行一些编辑,以使服务与远程系统兼容。 在 Visual Studio 中,转到应用服务提供商的项目(示例中称为“AppServicesProvider”),并选择其 Package.appxmanifest 文件。 右键单击并选择“ 查看代码 ”以查看文件的完整内容。 在主 应用程序 元素内创建 Extensions 元素(或找到它,如果它已存在)。 然后 创建扩展 以将项目定义为应用服务并引用其父项目。
...
<Extensions>
<uap:Extension Category="windows.appService" EntryPoint="RandomNumberService.RandomNumberGeneratorTask">
<uap3:AppService Name="com.microsoft.randomnumbergenerator"/>
</uap:Extension>
</Extensions>
...
在 AppService 元素旁边,添加 SupportsRemoteSystems 属性:
...
<uap3:AppService Name="com.microsoft.randomnumbergenerator" SupportsRemoteSystems="true"/>
...
若要使用此 uap3 命名空间中的元素,必须在清单文件顶部添加命名空间定义(如果它尚未存在)。
<?xml version="1.0" encoding="utf-8"?>
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3">
...
</Package>
然后生成应用服务提供商项目并将其部署到主机设备。
从客户端设备定位应用服务
要从中调用远程应用服务的设备需要具有远程系统功能的应用。 这可以添加到在主机设备上提供应用服务的同一应用中(在这种情况下,将在两台设备上安装同一应用),或在完全不同的应用中实现。
若要运行 as-is,此部分中的代码需要使用以下 语句
using Windows.ApplicationModel.AppService;
using Windows.System.RemoteSystems;
必须首先实例化 AppServiceConnection 对象,就像在本地调用应用服务一样。 创建 和使用应用服务中详细介绍了此过程。 在此示例中,要面向的应用服务是随机数生成器服务。
注释
假定已在代码中通过某种方式获取了 RemoteSystem 对象,以便调用接下来的方法。 有关如何设置远程应用的说明,请参阅 “启动远程应用 ”。
// This method returns an open connection to a particular app service on a remote system.
// param "remotesys" is a RemoteSystem object representing the device to connect to.
private async void openRemoteConnectionAsync(RemoteSystem remotesys)
{
// Set up a new app service connection. The app service name and package family name that
// are used here correspond to the AppServices UWP sample.
AppServiceConnection connection = new AppServiceConnection
{
AppServiceName = "com.microsoft.randomnumbergenerator",
PackageFamilyName = "Microsoft.SDKSamples.AppServicesProvider.CS_8wekyb3d8bbwe"
};
接下来,将为预期的远程设备创建 RemoteSystemConnectionRequest 对象。 然后,它用于向该设备打开 AppServiceConnection。 请注意,在下面的示例中,为了简洁起见,错误处理和报告大大简化了。
// a valid RemoteSystem object is needed before going any further
if (remotesys == null)
{
return;
}
// Create a remote system connection request for the given remote device
RemoteSystemConnectionRequest connectionRequest = new RemoteSystemConnectionRequest(remotesys);
// "open" the AppServiceConnection using the remote request
AppServiceConnectionStatus status = await connection.OpenRemoteAsync(connectionRequest);
// only continue if the connection opened successfully
if (status != AppServiceConnectionStatus.Success)
{
return;
}
此时,您应该已经与远程计算机上的应用服务建立了连接。
通过远程连接传递服务特定的消息
在这里,你可以以 ValueSet 对象的形式发送和接收来自服务的消息(有关详细信息,请参阅 创建和使用应用服务)。 随机数生成器服务采用两个包含键 "minvalue" 的整数并 "maxvalue" 作为输入,随机选择其范围内的整数,并使用键 "Result"将其返回到调用进程。
// create the command input
ValueSet inputs = new ValueSet();
// min_value and max_value vars are obtained somewhere else in the program
inputs.Add("minvalue", min_value);
inputs.Add("maxvalue", max_value);
// send input and receive output in a variable
AppServiceResponse response = await connection.SendMessageAsync(inputs);
string result = "";
// check that the service successfully received and processed the message
if (response.Status == AppServiceResponseStatus.Success)
{
// Get the data that the service returned:
result = response.Message["Result"] as string;
}
}
现在,你已连接到目标主机设备上的应用服务,在该设备上运行某个操作,并在客户端设备上收到数据。
相关主题
连接的应用和设备(Project Rome)概述
启动远程应用
创建和使用应用服务
远程系统 API 参考
远程系统示例