我目前在決議一張適應性卡片時遇到了以下問題。
這是一張卡:
{
"type": "AdaptiveCard"。
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"版本"。"1.4",
"body": [
{
"type": "TextBlock"。
"text": "{{DATE(${$root.AdditionalData['DUE-DATE']},COMPACT)}}"。
"wrap"。true。
}
]
}
這是卡片的內容:
{
"AdditionalData"/span>: {
"Due-Date": "2021-09-10T16:29:59Z"
}
代碼。
c# on .NET Framework 4.7.2 其中layout是一個具有上述卡片的字串,content是一個具有上述卡片內容的字串:
AdaptiveCardTemplate template = new AdaptiveCardTemplate(layout)。
string cardJson = template.Expand(content);
AdaptiveCardParseResult card = AdaptiveCard.FromJson(cardJson)。
然后它就崩潰了:
AdaptiveCards.AdaptiveSerializationException: '錯誤讀取字串。意外的令牌。未定義。路徑'text',第1行,位置137'。
JsonReaderException。讀取字串時出錯。未預期的標記。未定義。路徑'text',第1行,位置137。
在cardJson上生成的JSON在我看來在text屬性上是錯誤的:
{"type"/span>:"AdaptiveCard"/span>,"$schema"/span>:"http://adaptivecards.io/schemas/adaptive-card. json","version":"1. 4"/span>,"body"/span>:[{"type"/span>: "TextBlock","text":,"wrap":true}]}。
我正在使用適應性卡片nuget包:
- AdaptiveCards 2.7.2 。
- AdaptiveCards.Templating 1.2. 。
我是否遇到了一個決議錯誤?文本屬性的值應該是10.9.2021.
。在 adaptivecards.io 上的設計器中,出于某種原因,一切都運行正常。 有沒有人有一個修復/解決方法?
uj5u.com熱心網友回復:
如果你想讓字面意思"text": "10.9.2021"出現在你的cardJson中,使用"${formatDateTime(AdditionalData['DUE-DATE'], 'd.M.yyyy')}"來為你的TextBlock生成所需值:
{<
"type": "AdaptiveCard",
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.4",
"body": []。
{>
"type": "TextBlock",
"text": "${formatDateTime(AdditionalData['DUE-DATE'], 'd.M.yyyy')}",
"wrap": true
}
]
}
這使得所有的日期格式化都由AdaptiveCardTemplate來執行,結果是:
{}。
"type":"AdaptiveCard",
"$schema":"http://adaptivecards.io/schemas/adaptive-card.json",
"version":"1.4",
"body":[/span>
{>
"type":"TextBlock",
"text":"10.9.2021",
"wrap":true。
}
]
}
示范性操作#1 這里。
如果你希望在你的cardJson中使用"{{DATE(2021-09-10T16:29:59Z, COMPACT)}}",該將日期格式化委托給TextBlock,請使用:
{}。
"type": "AdaptiveCard",
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.4",
"body": []。
{>
"type": "TextBlock",
"text": "{{DATE(${AdditionalData['DUE-DATE']}, COMPACT)}}",
"wrap": true
}
]
}
其結果是
"text"/span>。 "{{DATE(2021-09-10T16:29:59Z, COMPACT)}}",
示范性操作#2 這里.
。注釋:
根據Microsoft 檔案:
- 使用Dot-notation來訪問物件層次結構的子物件。例如,
。${myParent.myChild}- 使用Indexer語法來通過鍵或陣列中的專案來檢索屬性。例如,
。${myArray[0]}但是,當訪問名稱中帶有連字符(或其他保留運算子)的物件屬性時,顯然需要使用索引器語法
['DUE-DATE']而不是 Dot-notation 來檢索其值,將單引號字串中的屬性名稱作為索引器傳遞。- 使用Dot-notation來訪問物件層次結構的子物件。例如,
根據docs
有一些保留的關鍵字可以訪問各種系結范圍。...
"$root"/span>。"根資料物件。當迭代到父物件時很有用"。因此,在使用Dot-notation時,當訪問當前范圍內的物件(默認為根)的屬性時,你不需要使用
$root。 如果出于某種原因,你需要或想要直接處理根物件,你可以像這樣使用$root:"text": "${formatDateTime($root. AdditionalData['DUE-DATE'], 'd.M.yyyy')}",Demo fiddle #3 here.
然而,似乎使用
$root與{{DATE()}}相結合會導致生成畸形的JSON。 例如:"text": "{{DATE(${$root. AdditionalData['DUE-DATE']}, COMPACT)}}",結果是
"text":,如你的問題所示。示范性操作 #4 此處。
這看起來是框架中的一個錯誤。 可能是決議器被
${$標記的序列噎住了,因為你的問題有點類似于Issue #6026: [Authoring][.NET][Templating] 在自適應卡片模板中訪問$root內的$when屬性時的不一致性,報告說無法決議"$when"。"${$root.UserName != null}"正確。你可以通過完全省略
$root,或者通過將$root.AdditionalData['DUE-DATE']包裹在一個額外的formatDateTime()中來避免這個問題,就像這樣:"text": "{{DATE(${formatDateTime($root. AdditionalData['DUE-DATE'])}, COMPACT)}}",結果是
"text": "{{DATE(2021-09-10T16:29:59. 000Z, COMPACT)}}",示范性忽悠#5 這里.
。從檔案頁面自適應卡片模板SDKs。故障排除:
Q. 為什么RFC3389格式的日期/時間,例如 "2017-02-14T06:08:00Z",當與模板一起使用時,不能與TIME/DATE函式一起使用? 答:.NET sdk nuget 1.0.0-rc.0版本表現出這種行為。這種行為在后續版本中得到糾正... 請使用 formatDateTime() 函式將日期/時間字串格式化為 RFC 3389,如這個例子中所示,或者你可以繞過 TIME/DATE 函式,直接使用 formatDateTime() 。關于formatDateTime()的更多資訊,請訪問這里。
。雖然這個使用
formatDateTime的建議是為了修復 1.0.0-rc.0 中的一個問題,但這個技巧也解決了上面注釋 #2 中提到的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/322785.html
標籤:
