概述

Windows Developer Day 在 Modern Application Experience 环节展示了一种可以让开发者以更通用和统一的方式来对卡片对展示和交互的方式,那就是:Adaptive Cards.

早在 Microsoft Build 2017,Matt Hidinger 就对 Adaptive Cards 做了展示。

而在 WDD 前夕,Adaptive Cards 1.0 版本正式 Release,开源在 GitHub Microsoft AdaptiveCards官网文档在 Microsoft Doc Adaptive Cards.

基本原理

那么 Adaptive Cards 是怎么工作的呢?

  • 卡片的制作者使用 JSON 或 SDK 中类构建的方式来描述卡片内容,包括文本,按钮,图片,链接等;
  • 卡片内容在宿主程序中完成渲染,宿主程序样式也是 JSON 或 SDK 类构建方式,样式包括内容大小,颜色等的定义;
  • 因为卡片的内容准备和 UI 渲染都可以完全通过 JSON 方式定义,所以使用 Adaptive Cards 各平台 SDK,就可以使用一套 JSON 完成多平台的通用和统一;

这种实现方式和 Adaptive Cards 要实现的目标也是一致的:

The goals for adaptive cards are:

. Portable - To any app, device, and UI framework

. Open - Libraries and schema are open source and shared

. Low cost - Easy to define, easy to consume

. Expressive - Targeted at the long tail of content that developers want to produce

. Purely declarative - No code is needed or allowed

. Automatically styled - To the Host application UX and brand guidelines

开发体验

多平台 SDK 支持

因为 Adaptive Cards 是一种跨平台方案,所以官方提供了 JavaScript,Android,iOS,UWP 和 .NET 五种常用的原生 SDK 来实现集成。

Install Adaptive Cards SDK

Platform Install Build Docs
 JavaScript npm v1.0.0 Source Docs
 .NET nuget v1.0.0 Source Docs
 .NET WPF nuget v1.0.0 Source Docs
 .NET HTML nuget v1.0.0 Source Docs
 Windows UWP nuget v1.0.0 Source Docs
 Android maven-central v1.0.0 Source Docs
 iOS pod v1.0.0 Source Docs

而目前  Adaptive Cards 支持的平台:

  • 已经在线可用的:Bot Framework - WebChat,Cortana Skills,Windows Timeline
  • 还在预览状态的:Skype,Outlook,Microsoft Teams,Windows Notifications,Bot Framework - Other Channels

UWP 示例开发

1. 通过 Nuget 方式在 PM 中添加包:

Install-Package AdaptiveCards.Rendering.Uwp -Version ..

2. 实例化一个 Renderer,这个 Renderer 被用来渲染宿主配置信息和卡片内容:

using AdaptiveCards.Rendering.Uwp;
...
var renderer = new AdaptiveCardRenderer();

3. 为卡片设置宿主配置:

示例中我使用一个 ComboBox 来切换宿主配置,从不同的文本文件读取对应的 JSON 字符串,反序列化为 HostConfig 并赋值给 Renderer。

string configJson = await getCardString(string.Format(@"Assets\{0}", (hostConfigBox.SelectedItem as ComboBoxItem).Content.ToString()));
var hostConfig = AdaptiveHostConfig.FromJsonString(configJson);
renderer.HostConfig = hostConfig.HostConfig;

4. 设置卡片内容:

示例中我从文本文件中读取内容对应的 JSON 字符串,反序列化为 AdaptiveCard 类实例。

string cardJson = await getCardString(@"Assets\card.txt");
var card = AdaptiveCard.FromJsonString(cardJson).AdaptiveCard;
RenderedAdaptiveCard renderResult = renderer.RenderAdaptiveCard(card);

5. 在界面中显示卡片:

把卡片内容显示在界面的 Grid 中,每次显示时,先清空前面的显示内容。

if (renderResult.FrameworkElement != null)
{
var uiCard = renderResult.FrameworkElement;
uiCard.HorizontalAlignment = HorizontalAlignment.Left;
cardGrid.Children.Clear();
cardGrid.Children.Add(uiCard);
}

来看看运行的效果:

可以看到,使用同样的卡片内容,在切换不同的宿主配置 Skype 和 Microsoft Teams 时,对应的卡片渲染后的 UI 是不同的,也是符合各自宿主 UI 风格的。

UWP SDK 的使用过程基本就是这样,非常的简单易上手。我们来看一下中间两个重要的类:AdaptiveCard 和 AdaptiveHostConfig.

AdaptiveCard:

这个类里,我们看到了我们用到 FromJson 方法,以及一些主要属性:Version(用于标识更新版本),Speak (表示卡片的朗读内容),FallbackText(后备文本),BackgroundImage(卡片背景图片),Actions(按钮的操作集合)等。

#region 程序集 AdaptiveCards.Rendering.Uwp, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime
// C:\Users\123\.nuget\packages\AdaptiveCards.Rendering.Uwp\1.0.0\lib\uap10.0\AdaptiveCards.Rendering.Uwp.winmd
#endregion using System;
using System.Collections.Generic;
using Windows.Data.Json;
using Windows.Foundation.Metadata; namespace AdaptiveCards.Rendering.Uwp
{
[Activatable()]
[Static(typeof(IAdaptiveCardStatics), )]
[Version()]
public sealed class AdaptiveCard : IAdaptiveCard
{
public AdaptiveCard(); public JsonObject ToJson();
[Overload("FromJson")]
public static AdaptiveCardParseResult FromJson(JsonObject adaptiveJson);
[Overload("FromJsonWithParserRegistration")]
public static AdaptiveCardParseResult FromJson(JsonObject adaptiveJson, AdaptiveElementParserRegistration elementRegistration, AdaptiveActionParserRegistration actionRegistration);
[Overload("FromJsonString")]
public static AdaptiveCardParseResult FromJsonString(string adaptiveJson);
[Overload("FromJsonStringWithParserRegistration")]
public static AdaptiveCardParseResult FromJsonString(string adaptiveJson, AdaptiveElementParserRegistration elementRegistration, AdaptiveActionParserRegistration actionRegistration); public string Version { get; set; }
public ContainerStyle Style { get; set; }
public string Speak { get; set; }
public string FallbackText { get; set; }
public Uri BackgroundImage { get; set; }
public IList<IAdaptiveActionElement> Actions { get; }
public IList<IAdaptiveCardElement> Body { get; }
public ElementType ElementType { get; }
}
}

而针对 AdaptiveCard 的格式, 完整的说明文档可以在官方文档的 Card Schema 中看到:https://docs.microsoft.com/zh-cn/adaptive-cards/create/cardschema

对应上面的示例,我们使用的 JSON 文件大致组成如下:

AdaptiveHostConfig:

这里类里,我们看到了我们用到的 FromJson 方法,以及设置宿主样式的配置信息,如字体,文字大小,按钮操作,文字间距等样式配置。大家也可以再去具体看每个配置都有哪些枚举值可用。

#region 程序集 AdaptiveCards.Rendering.Uwp, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime
// C:\Users\123\.nuget\packages\AdaptiveCards.Rendering.Uwp\1.0.0\lib\uap10.0\AdaptiveCards.Rendering.Uwp.winmd
#endregion using Windows.Data.Json;
using Windows.Foundation.Metadata; namespace AdaptiveCards.Rendering.Uwp
{
[Activatable()]
[Static(typeof(IAdaptiveHostConfigStatics), )]
[Version()]
public sealed class AdaptiveHostConfig : IAdaptiveHostConfig
{
public AdaptiveHostConfig(); public static AdaptiveHostConfigParseResult FromJsonString(string hostConfigJson);
public static AdaptiveHostConfigParseResult FromJson(JsonObject hostConfigJson); public bool SupportsInteractivity { get; set; }
public AdaptiveSpacingConfig Spacing { get; set; }
public AdaptiveSeparatorConfig Separator { get; set; }
public AdaptiveImageSizesConfig ImageSizes { get; set; }
public AdaptiveImageSetConfig ImageSet { get; set; }
public AdaptiveImageConfig Image { get; set; }
public AdaptiveFontWeightsConfig FontWeights { get; set; }
public AdaptiveFontSizesConfig FontSizes { get; set; }
public string FontFamily { get; set; }
public AdaptiveFactSetConfig FactSet { get; set; }
public AdaptiveContainerStylesDefinition ContainerStyles { get; set; }
public AdaptiveCardConfig AdaptiveCard { get; set; }
public AdaptiveActionsConfig Actions { get; set; }
}
}

而针对 AdaptiveHostConfig 的字段, 完整的说明文档可以在官方文档的 Card Schema 中看到:https://docs.microsoft.com/zh-cn/adaptive-cards/display/hostconfig

对应上面的示例,我们使用的 JSON 文件大致组成如下:

如果大家想简单体验一下 AdaptiveCard 和 AdaptiveHostConfig 的变化对卡片的影响,不想自己写 Demo,也可以通过它提供的在线体验的方式:http://adaptivecards.io/visualizer/index.html?hostApp=Bot%20Framework%20WebChat

通过这个在线编辑器,可以很直观的看到每个字段的修改对卡片的影响。

对 Adaptive Cards 的简单体验和示例就到这里,后面如果产品代码中实际用到,我会再结合实际场景来具体展开分析,谢谢大家!

Windows Developer Day - Adaptive Cards的更多相关文章

  1. Windows Developer Day Review

    北京时间 3 月 8 日凌晨 1 点钟,今年的第一次 Windows Developer Day 正式召开.    因为时间太晚看不了直播,我也是第二天早上在公司看的重播.整个会议过程有很多值得去研究 ...

  2. Windows Developer Day - MSIX and Advanced Installer

    前面一篇我们介绍了 Adaptive Cards 的基础知识,而在 Windows Developer Day 的 Modern Application Experience 环节,还有一个需要划重点 ...

  3. Windows Developer Day - Windows AI Platform

    本次 Windows Developer Day,最值得期待的莫过于 Windows AI Platform 了,可以说是千呼万唤始出来.观看直播的开发者们,留言最多的也是 Windows AI Pl ...

  4. About Windows 10 SDK Preview Build 17110

    在 Windows Developer Day 活动同时,微软正式 Release 了 Windows 10 SDK Preview Build 17110. Windows 10 SDK Previ ...

  5. 使用 .NET 平台,如何玩转 Universal Windows 应用?

    2015年7月30日 本文作者是 Managed Languages 团队项目经理 Lucian Wischik. 不久前,Visual Studio 2015上新增 Windows 10 应用的开发 ...

  6. 《Windows IoT 应用开发指南》

    物物互联的时代已经到来,智能家居.智慧校园.智慧交通.可穿戴.无人机.全息投影,各种各样的新名词.黑科技层出不穷.当我们为五年前能够通过手机控制家电而欣喜若狂的时候,可曾憧憬过当前使用增强现实设备完成 ...

  7. Wintel物联网平台-Windows IoT新手入门指南

    1. 引言 近期,微软跟进物联网的速度也在不断加速,除了微软手环,.NET MicroFramework,还有一个叫做Windows IoT的项目.该项目早在今年4月份的Build大会上就提出来了,7 ...

  8. 运用JavaScript构建你的第一个Metro式应用程序(on Windows 8)(一)

    原文 http://blog.csdn.net/zhangxin09/article/details/6784547 作者:Chris Sells 译: sp42   原文 包括 HTML.CSS 和 ...

  9. Tutorial: Create a Windows Machine Learning UWP application (C#)

    In this tutorial, we'll build a simple Universal Windows Platform application that uses a trained ma ...

随机推荐

  1. python入门学习笔记(二)

    6.6替换元素 7.tuple类型 7.1创建tuple 7.2创建单元素tuple    7.3"可变"的tuple 8.条件判断和循环 8.1,if语句   8.2,if... ...

  2. 前端构建工具之gulp的安装和配置

    在选择构建工具时,看到更多人推荐gulp,从此入了gulp的坑- 一.安装node环境 百度谷歌一下就有了,在终端中分别输入 node -v 和 npm -v,若显示node和npm的版本号则说明no ...

  3. CentOS7上安装Nginx、PHP、MySQL

    一.安装准备 首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++.gcc.openssl-devel.pcre-devel和zlib ...

  4. 剑指offer面试题-Java版-持续更新

    最近在用Java刷剑指offer(第二版)的面试题.书中原题的代码采用C++编写,有些题的初衷是为了考察C++的指针.模板等特性,这些题使用Java编写有些不合适.但多数题还是考察通用的算法.数据结构 ...

  5. Qt 5.9.4 如何静态编译和部署?

    Qt 5.9.4 如何静态编译和部署? MSVC2015 x86 静态编译 Qt 部署静态库 VS2015 部署静态库 1. MSVC2015 x86 静态编译 1.1 Qt 官网下载最新源代码 立即 ...

  6. 《android开发艺术探索》读书笔记(十二)--Bitmap的加载和Cache

    接上篇<android开发艺术探索>读书笔记(十一)--Android的线程和线程池 No1: 目前比较常用的缓存策略是LruCache和DiskLruCache,LruCache常被用作 ...

  7. $_SERVER变量

    $_SERVER is an array containing information such as headers, paths, and script locations. The entrie ...

  8. 学习PHP的必备开发工具

    对于PHP开发者,在互联网上有很多可用的开发工具,但对于初学者不知道哪个php开发工具比较好,找到一个合适的PHP开发工具是很难的,需要花费很多的时间精力.所以,今天常青春工作室就为初学者推荐几个最好 ...

  9. Linux常用软件

    网络应用 即时聊天 pidgin 支持多协议,如msn, yahoo, icq, irc ... eva QQ 聊天客户端,KDE程序,推荐 Skype 网络电话,网络聊天,推荐 lumaqq Jav ...

  10. redis的密码设置(windows与linux相同)

    接着我们昨天的说,昨天redis的启动已经了解,今天来说说redis的密码设置.(不管怎么说redis也是数据库,也需要密码) 修改密码可以2种行径.第一种,直接修改配置文件,打开redis.conf ...