用于创作卡的 .NET SDK

入门页中所述,自适应卡片是 JSON 对象模型。 .NET 库使使用该 JSON 变得更加容易。

NuGet安装

NuGet AdaptiveCards 包提供用于在 .NET 中使用自适应卡的类型

Nuget install

Install-Package AdaptiveCards

示例:创建 AdaptiveCard 并将其序列化为 JSON

此示例演示如何使用标准 C# 对象生成自适应卡片,然后将其序列化为 JSON 以通过线路传输。

using AdaptiveCards;
// ...

AdaptiveCard card = new AdaptiveCard(new AdaptiveSchemaVersion(1, 0));

card.Body.Add(new AdaptiveTextBlock() 
{
    Text = "Hello",
    Size = AdaptiveTextSize.ExtraLarge
});

card.Body.Add(new AdaptiveImage() 
{
    Url = new Uri("http://adaptivecards.io/content/cats/1.png")
});

// serialize the card to JSON
string json = card.ToJson();

示例:从 JSON 分析 AdaptiveCard

此示例演示如何将 JSON 有效负载分析为自适应卡片。 这样,就可以使用 呈现器 SDK 轻松操作对象模型,甚至可以在应用中呈现自适应卡片。

try
{
    // Get a JSON-serialized payload
    // Your app will probably get cards from somewhere else :)
    var client = new HttpClient();
    var response = await client.GetAsync("http://adaptivecards.io/payloads/ActivityUpdate.json");
    var json = await response.Content.ReadAsStringAsync();

    // Parse the JSON 
    AdaptiveCardParseResult result = AdaptiveCard.FromJson(json);

    // Get card from result
    AdaptiveCard card = result.Card;

    // Optional: check for any parse warnings
    // This includes things like unknown element "type"
    // or unknown properties on element
    IList<AdaptiveWarning> warnings = result.Warnings;
}
catch(AdaptiveSerializationException ex)
{
    // Failed to deserialize card 
    // This occurs from malformed JSON
    // or schema violations like required properties missing 
}