UWP App Services in Windows 10
1.AppServices in Universal Windows Platform(UWP)
UWP 应用服务可以提供给另一个UWP应用。在Win10系统中,一个应用服务当作应用的一个方法和机制来提供服务给其他应用。
一个后台任务的应用服务工作的形式(你可以在后台跑代码,通过后台接口实现)。前台客户端可以引用另一个应用的服务在后台中执行任务。
应用服务就像web 服务一样在Win10设备中使用。
首先,让我们来创建一个 Windows Runtime Component (Universal Windows) 项目 命名"MyCalculatorService"。 这会是我们的计算服务应用。
创建一个以Calculator 为类名的 实现 IBackgroundTask接口。在WCF技术中,你可以说这个类是我们的带操作契约的服务契约
public sealed class Calculator : IBackgroundTask
{
private BackgroundTaskDeferral backgroundTaskDeferral;
private AppServiceConnection appServiceConnection;
public void Run(IBackgroundTaskInstance taskInstance)
{
this.backgroundTaskDeferral = taskInstance.GetDeferral(); var details = taskInstance.TriggerDetails as AppServiceTriggerDetails;
appServiceConnection = details.AppServiceConnection; appServiceConnection.RequestReceived += OnRequestReceived;
taskInstance.Canceled += OnTaskCanceled;
} private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
var messageDeferral = args.GetDeferral();
ValueSet message = args.Request.Message;
ValueSet returnData = new ValueSet(); string command = message["Command"] as string; //Add, Subtract, Multiply, Divide
int? firstNumber = message["Input1"] as int?;
int? secondNumber = message["Input2"] as int?;
int? result = ; if (firstNumber.HasValue && secondNumber.HasValue)
{
switch (command)
{
case "Add":
{
result = firstNumber + secondNumber;
returnData.Add("Result", result.ToString());
returnData.Add("Status", "Complete");
break;
}
case "Subtract":
{
result = firstNumber - secondNumber;
returnData.Add("Result", result.ToString());
returnData.Add("Status", "Complete");
break;
}
case "Multiply":
{
result = firstNumber * secondNumber;
returnData.Add("Result", result.ToString());
returnData.Add("Status", "Complete");
break;
}
case "Divide":
{
result = firstNumber / secondNumber;
returnData.Add("Result", result.ToString());
returnData.Add("Status", "Complete");
break;
}
default:
{
returnData.Add("Status", "Fail: unknown command");
break;
}
}
} await args.Request.SendResponseAsync(returnData);
messageDeferral.Complete();
} private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
if (this.backgroundTaskDeferral != null)
{
this.backgroundTaskDeferral.Complete();
}
}
}
现在 下一步 来创建一个项目来暴露这个功能在客户端应用(类似WCF环境的宿主)
创建UWP项目 命名MyCalculatorServiceProvider。导入 MyCalculatorService 项目从引用里。
现在打开MyCalculatorServiceProvider 项目的Package.appxmanifest文件。右键打开,找到申明应用服务实例命名CalculatorService1 ,
在entry point 中键入MyCalculatorService.Calculator (看上面的代码,这是实现IBackgroundTask的类)

最后一步来运行创建的这个服务客户端,这个服务提供的应用必须要部署 在你调用服务之前。你也需要这个服务应用(MyCalculatorServiceProvider )的包名,为了调用他。
在创建一个UWP项目命名 CalculatorClient。你会注意到这个名字是以服务名定义的, 为了得到PackageFamilyName 我们有两种方法。
第一个方法:通过调用 Windows.ApplicationModel.Package.Current.Id.FamilyName
包含在 MyCalculatorServiceProvider 项目中可以跑一下。
第二个方法:找到部署MyCalculatorServiceProvider 项目的全路径
图片选中的 d1238b67-b1ef-4782-9f3e-df718d97e378_8wekyb3d8bbwe 这个就是 PackageFamilyName
这是核心代码
public sealed partial class MainPage : Page
{
private AppServiceConnection calculatorService; public MainPage()
{
this.InitializeComponent();
} private async void button_Click(object sender, RoutedEventArgs e)
{
//Add the connection
if (calculatorService == null)
{
calculatorService = new AppServiceConnection();
calculatorService.AppServiceName = "CalculatorService1";
calculatorService.PackageFamilyName = "83da5395-2473-49fb-b361-37072e87e9b9_xe3s0d4n4696a"; var status = await calculatorService.OpenAsync(); if (status != AppServiceConnectionStatus.Success)
{
Result.Content = "Failed to connect";
return;
}
} //Call the service
int num1 = int.Parse(InputtextBox1.Text);
int num2 = int.Parse(InputtextBox2.Text);
var message = new ValueSet(); message.Add("Command", Operation.SelectionBoxItem);
message.Add("Input1", num1);
message.Add("Input2", num2); AppServiceResponse response = await calculatorService.SendMessageAsync(message);
string result = ""; if (response.Status == AppServiceResponseStatus.Success)
{
//Get the data that the service sent
if (response.Message["Status"] as string == "Complete")
{
result = response.Message["Result"] as string;
}
}
message.Clear();
ResulttextBlock.Text = result;
}
}

4.先安装应用服务(服务寄宿在UWP应用里,部署即可),在安装客户端。
5.打开客户端开始计算,就好找到本台机器应用服务程序。(只限同一台机器)
引用:
如果要使用UWP在本台计算机访问其他电脑的WCF服务的画,在App.config文件里修改baseAddress地址为192.168.X.X 这样才能找到
请看WCF文章
UWP App Services in Windows 10的更多相关文章
- 1)Win10-UWA开发 UWP应用操作方法、Windows 10应用程序的指南
孙广东 2015.8.22 全部任务类型(比方在列表中显示数据或创建导航窗格)的说明和代码演示样例. 在这一节 包含例如以下: 主题 描写叙述 Accessibility 创建通用的Windows ...
- Windows 10 后台音频
UWP版本的网易云音乐已经上架,虽然还不支持Windows Phone但是整体而言功能已经比较齐全了! 那么如何在Windows 10 UWP实现后台播放呢? 我之前是一直在做Windows Phon ...
- 打包一个UWP APP
Before packaging your app Test your app. Before you package your app for store submission, make sure ...
- 基于Windows 10平台的PM2.5检测器制作
本篇文章详细讲解了如何利用SDS011激光式PM2.5传感器.HC-06蓝牙模块和Windows 10设备完成一个简单的PM2.5检测器及其应用程序的开发.该检测器使用蓝牙完成数据输出,方便设备连接, ...
- UWP深入学习六:Build better apps: Windows 10 by 10 development series
Promotion in the Windows Store In this article, I walk through how to Give your Store listing a mak ...
- New Windows 10 SDK - Multi-instance UWP apps
概述 前面一篇 About Windows 10 SDK Preview Build 17110 中,我们简单介绍了 Multi-instance UWP Apps,今天结合开发过程详细讲解一下. 在 ...
- 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互
[源码下载] 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互 作者: ...
- Windows 10 UWP程序标题栏设置
在Windows 10程序中,以前只能用于全屏方式的Metro程序现在可以运行在窗口模式下了,并且改了个新名字,叫Windows 通用程序(Universal Windows app),简称UWP程序 ...
- 使用 Microsoft.UI.Xaml 解决 UWP 控件和对老版本 Windows 10 的兼容性问题
原文 使用 Microsoft.UI.Xaml 解决 UWP 控件和对老版本 Windows 10 的兼容性问题 虽然微软宣称 Windows 10 将是最后一个 Windows 版本,但由于年代跨越 ...
随机推荐
- codeforces 467C George and Job(简单dp,看了题解抄一遍)
题目 参考了网页:http://www.xue163.com/exploit/180/1802901.html //看了题解,抄了一遍,眼熟一下,增加一点熟练度 //dp[i][j]表示是前i个数选出 ...
- PHP中把数据库查询结果输出为json格式
<?php header("Content-type:text/html;charset=utf-8");//字符编码设置 $servername = "local ...
- ubuntu 配置lamp
官方配置网站:http://wiki.ubuntu.org.cn/LAMP_%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%A ...
- python笔记之json报错
写爬虫的过程中不免遇到处理json数据的情况,今天在爬取新华网新闻数据时发现使用json.loads函数时报错: json.decoder.JSONDecodeError: Expecting val ...
- 炒了8年的概念,到底该如何理解DevOps这个词?
什么是DevOps及误区 DevOps概念从2009年提出已有8个年头.可是在8年前的那个时候,为什么DevOps没有迅速走红呢?即便是在2006年Amazon发布了ECS,微软在2008年和2010 ...
- mongodb drop不释放磁盘空间
点击(此处)折叠或打开 use demodb //使用demodb,以下假设操作的collection是foo db.foo.remove({"id":"123456&q ...
- SpringBoot中logback.xml使用application.yml中属性
教你如何使用 springProfile 与 springProperty 让你的logback.xml 配置显得更有逼格,当别人还在苦苦挣扎弄logback-{profile}.xml的时候 你一个 ...
- POJ 3537
利用后继节点的SG值求出当前的SG值. 在当前任意一个BLANK插入一个x后,分成两段,于是,看成两段的NIM,异或和,按SG的定义求出当前的SG值即可. #include <iostream& ...
- 程序中的文件之沙盒以及plist文件的初步使用
沙盒是相对于"应用程序"的文件,也就是相相应app所在的页面的文件. 每个应用都有自己的应用沙盒(应用沙盒就是文件系统文件夹).与其它文件系统隔离.应用必须呆在在积极的沙盒中.其它 ...
- 【cl】eclipse配置svn
查看Eclipse版本号 http://jingyan.baidu.com/article/020278118660e81bcd9ce545.html Window>preferences输入S ...